Commit 46832371 by Sagar Thakur Committed by Jim Stichnoth

[Subzero][MIPS32] Fix alloca alignment and offset for Om1 and O2 optimization

R=stichnot@chromium.org Review URL: https://codereview.chromium.org/2417233002 . Patch from Sagar Thakur <sagar.thakur@imgtec.com>.
parent ec929173
...@@ -216,6 +216,25 @@ uint32_t TargetMIPS32::getStackAlignment() const { ...@@ -216,6 +216,25 @@ uint32_t TargetMIPS32::getStackAlignment() const {
return MIPS32_STACK_ALIGNMENT_BYTES; return MIPS32_STACK_ALIGNMENT_BYTES;
} }
uint32_t TargetMIPS32::getCallStackArgumentsSizeBytes(const InstCall *Call) {
TargetMIPS32::CallingConv CC;
RegNumT DummyReg;
size_t OutArgsSizeBytes = 0;
for (SizeT i = 0, NumArgs = Call->getNumArgs(); i < NumArgs; ++i) {
Operand *Arg = legalizeUndef(Call->getArg(i));
const Type Ty = Arg->getType();
RegNumT RegNum;
if (CC.argInReg(Ty, i, &RegNum)) {
continue;
}
OutArgsSizeBytes = applyStackAlignmentTy(OutArgsSizeBytes, Ty);
OutArgsSizeBytes += typeWidthInBytesOnStack(Ty);
}
return applyStackAlignment(OutArgsSizeBytes);
}
void TargetMIPS32::genTargetHelperCallFor(Inst *Instr) { void TargetMIPS32::genTargetHelperCallFor(Inst *Instr) {
constexpr bool NoTailCall = false; constexpr bool NoTailCall = false;
constexpr bool IsTargetHelperCall = true; constexpr bool IsTargetHelperCall = true;
...@@ -586,6 +605,7 @@ void TargetMIPS32::findMaxStackOutArgsSize() { ...@@ -586,6 +605,7 @@ void TargetMIPS32::findMaxStackOutArgsSize() {
} }
} }
} }
CurrentAllocaOffset = MaxOutArgsSizeBytes;
} }
void TargetMIPS32::translateO2() { void TargetMIPS32::translateO2() {
...@@ -1706,9 +1726,9 @@ void TargetMIPS32::PostLoweringLegalizer::legalizeMov(InstMIPS32Mov *MovInstr) { ...@@ -1706,9 +1726,9 @@ void TargetMIPS32::PostLoweringLegalizer::legalizeMov(InstMIPS32Mov *MovInstr) {
if (Var->isRematerializable()) { if (Var->isRematerializable()) {
// This is equivalent to an x86 _lea(RematOffset(%esp/%ebp), Variable). // This is equivalent to an x86 _lea(RematOffset(%esp/%ebp), Variable).
// ExtraOffset is only needed for frame-pointer based frames as we have // ExtraOffset is only needed for stack-pointer based frames as we have
// to account for spill storage. // to account for spill storage.
const int32_t ExtraOffset = (Var->getRegNum() == Target->getFrameReg()) const int32_t ExtraOffset = (Var->getRegNum() == Target->getStackReg())
? Target->getFrameFixedAllocaOffset() ? Target->getFrameFixedAllocaOffset()
: 0; : 0;
...@@ -2008,6 +2028,17 @@ void TargetMIPS32::lowerAlloca(const InstAlloca *Instr) { ...@@ -2008,6 +2028,17 @@ void TargetMIPS32::lowerAlloca(const InstAlloca *Instr) {
Context.insert<InstFakeDef>(Dest); Context.insert<InstFakeDef>(Dest);
return; return;
} }
if (Alignment > MIPS32_STACK_ALIGNMENT_BYTES) {
CurrentAllocaOffset =
Utils::applyAlignment(CurrentAllocaOffset, Alignment);
}
auto *T = I32Reg();
_addiu(T, SP, CurrentAllocaOffset);
_mov(Dest, T);
CurrentAllocaOffset += Value;
return;
} else { } else {
// Non-constant sizes need to be adjusted to the next highest multiple of // Non-constant sizes need to be adjusted to the next highest multiple of
// the required alignment at runtime. // the required alignment at runtime.
...@@ -2034,15 +2065,6 @@ void TargetMIPS32::lowerAlloca(const InstAlloca *Instr) { ...@@ -2034,15 +2065,6 @@ void TargetMIPS32::lowerAlloca(const InstAlloca *Instr) {
_mov(SP, Dest); _mov(SP, Dest);
return; return;
} }
// Add enough to the returned address to account for the out args area.
if (MaxOutArgsSizeBytes > 0) {
Variable *T = makeReg(getPointerType());
_addiu(T, SP, MaxOutArgsSizeBytes);
_mov(Dest, T);
} else {
_mov(Dest, SP);
}
} }
void TargetMIPS32::lowerInt64Arithmetic(const InstArithmetic *Instr, void TargetMIPS32::lowerInt64Arithmetic(const InstArithmetic *Instr,
...@@ -2322,6 +2344,12 @@ void TargetMIPS32::lowerInt64Arithmetic(const InstArithmetic *Instr, ...@@ -2322,6 +2344,12 @@ void TargetMIPS32::lowerInt64Arithmetic(const InstArithmetic *Instr,
void TargetMIPS32::lowerArithmetic(const InstArithmetic *Instr) { void TargetMIPS32::lowerArithmetic(const InstArithmetic *Instr) {
Variable *Dest = Instr->getDest(); Variable *Dest = Instr->getDest();
if (Dest->isRematerializable()) {
Context.insert<InstFakeDef>(Dest);
return;
}
// We need to signal all the UnimplementedLoweringError errors before any // We need to signal all the UnimplementedLoweringError errors before any
// legalization into new variables, otherwise Om1 register allocation may fail // legalization into new variables, otherwise Om1 register allocation may fail
// when it sees variables that are defined but not used. // when it sees variables that are defined but not used.
......
...@@ -104,7 +104,9 @@ public: ...@@ -104,7 +104,9 @@ public:
PrologEmitsFixedAllocas = true; PrologEmitsFixedAllocas = true;
} }
int32_t getFrameFixedAllocaOffset() const override { int32_t getFrameFixedAllocaOffset() const override {
return FixedAllocaSizeBytes - (SpillAreaSizeBytes - MaxOutArgsSizeBytes); int32_t FixedAllocaOffset =
Utils::applyAlignment(CurrentAllocaOffset, FixedAllocaAlignBytes);
return FixedAllocaOffset - MaxOutArgsSizeBytes;
} }
uint32_t maxOutArgsSizeBytes() const override { return MaxOutArgsSizeBytes; } uint32_t maxOutArgsSizeBytes() const override { return MaxOutArgsSizeBytes; }
...@@ -701,10 +703,7 @@ protected: ...@@ -701,10 +703,7 @@ protected:
void lowerSwitch(const InstSwitch *Instr) override; void lowerSwitch(const InstSwitch *Instr) override;
void lowerUnreachable(const InstUnreachable *Instr) override; void lowerUnreachable(const InstUnreachable *Instr) override;
void prelowerPhis() override; void prelowerPhis() override;
uint32_t getCallStackArgumentsSizeBytes(const InstCall *Instr) override { uint32_t getCallStackArgumentsSizeBytes(const InstCall *Instr) override;
(void)Instr;
return 0;
}
void genTargetHelperCallFor(Inst *Instr) override; void genTargetHelperCallFor(Inst *Instr) override;
void doAddressOptLoad() override; void doAddressOptLoad() override;
void doAddressOptStore() override; void doAddressOptStore() override;
...@@ -754,6 +753,7 @@ protected: ...@@ -754,6 +753,7 @@ protected:
bool VariableAllocaUsed = false; bool VariableAllocaUsed = false;
uint32_t MaxOutArgsSizeBytes = 0; uint32_t MaxOutArgsSizeBytes = 0;
uint32_t TotalStackSizeBytes = 0; uint32_t TotalStackSizeBytes = 0;
uint32_t CurrentAllocaOffset = 0;
static SmallBitVector TypeToRegisterSet[RCMIPS32_NUM]; static SmallBitVector TypeToRegisterSet[RCMIPS32_NUM];
static SmallBitVector TypeToRegisterSetUnfiltered[RCMIPS32_NUM]; static SmallBitVector TypeToRegisterSetUnfiltered[RCMIPS32_NUM];
static SmallBitVector RegisterAliases[RegMIPS32::Reg_NUM]; static SmallBitVector RegisterAliases[RegMIPS32::Reg_NUM];
......
...@@ -94,9 +94,9 @@ entry: ...@@ -94,9 +94,9 @@ entry:
; MIPS32-LABEL: fixed_416_align_32 ; MIPS32-LABEL: fixed_416_align_32
; MIPS32-OPT2: addiu sp,sp,-448 ; MIPS32-OPT2: addiu sp,sp,-448
; MIPS32-OPT2: addiu a0,sp,16 ; MIPS32-OPT2: addiu a0,sp,32
; MIPS32-OPTM1: addiu sp,sp,-448 ; MIPS32-OPTM1: addiu sp,sp,-448
; MIPS32-OPTM1: addiu [[REG:.*]],sp,16 ; MIPS32-OPTM1: addiu [[REG:.*]],sp,32
; MIPS32-OPTM1: sw [[REG]],{{.*}} ; MIPS32-OPTM1: sw [[REG]],{{.*}}
; MIPS32-OPTM1: lw a0,{{.*}} ; MIPS32-OPTM1: lw a0,{{.*}}
; MIPS32: jal {{.*}} R_{{.*}} f1 ; MIPS32: jal {{.*}} R_{{.*}} f1
...@@ -159,9 +159,9 @@ entry: ...@@ -159,9 +159,9 @@ entry:
; MIPS32-LABEL: fixed_351_align_32 ; MIPS32-LABEL: fixed_351_align_32
; MIPS32-OPT2: addiu sp,sp,-384 ; MIPS32-OPT2: addiu sp,sp,-384
; MIPS32-OPT2: addiu a0,sp,16 ; MIPS32-OPT2: addiu a0,sp,32
; MIPS32-OPTM1: addiu sp,sp,-384 ; MIPS32-OPTM1: addiu sp,sp,-384
; MIPS32-OPTM1: addiu [[REG:.*]],sp,16 ; MIPS32-OPTM1: addiu [[REG:.*]],sp,32
; MIPS32-OPTM1: sw [[REG]],{{.*}} ; MIPS32-OPTM1: sw [[REG]],{{.*}}
; MIPS32-OPTM1: lw a0,{{.*}} ; MIPS32-OPTM1: lw a0,{{.*}}
; MIPS32: jal {{.*}} R_{{.*}} f1 ; MIPS32: jal {{.*}} R_{{.*}} f1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment