Skip to content

Commit d31583d

Browse files
committedMay 6, 2015
[x86] Fix register class of folded load index reg.
When folding a load in to another instruction, we need to fix the class of the index register Otherwise, it could be something like GR64 not GR64_NOSP and would fail the machine verifier. llvm-svn: 236644
1 parent 0a648a4 commit d31583d

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
 

Diff for: ‎llvm/lib/Target/X86/X86FastISel.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -3534,6 +3534,23 @@ bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
35343534
if (!Result)
35353535
return false;
35363536

3537+
// The index register could be in the wrong register class. Unfortunately,
3538+
// foldMemoryOperandImpl could have commuted the instruction so its not enough
3539+
// to just look at OpNo + the offset to the index reg. We actually need to
3540+
// scan the instruction to find the index reg and see if its the correct reg
3541+
// class.
3542+
for (MIOperands MO(Result); MO.isValid(); ++MO) {
3543+
if (!MO->isReg() || MO->isDef() || MO->getReg() != AM.IndexReg)
3544+
continue;
3545+
// Found the index reg, now try to rewrite it.
3546+
unsigned OpNo = MO.getOperandNo();
3547+
unsigned IndexReg = constrainOperandRegClass(Result->getDesc(),
3548+
MO->getReg(), OpNo);
3549+
if (IndexReg == MO->getReg())
3550+
continue;
3551+
MO->setReg(IndexReg);
3552+
}
3553+
35373554
Result->addMemOperand(*FuncInfo.MF, createMachineMemOperandFor(LI));
35383555
FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
35393556
MI->eraseFromParent();

Diff for: ‎llvm/test/CodeGen/X86/fast-isel-movsbl-indexreg.ll

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
; RUN: llc %s -o - -verify-machineinstrs -fast-isel=true | FileCheck %s
2+
3+
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
4+
target triple = "x86_64-unknown-unknown"
5+
6+
; The index register on the folded memory operand was incorrect. Ensure we generate
7+
; movsbl in fast-isel, but also that it passes verification which will check the register
8+
; class.
9+
10+
; CHECK: movsbl
11+
12+
@table = external hidden global [64 x i8], align 16
13+
14+
define i32 @test(i32 %x, i64 %offset) {
15+
bb:
16+
%tmp37 = getelementptr inbounds [64 x i8], [64 x i8]* @table, i64 0, i64 %offset
17+
%tmp38 = load i8, i8* %tmp37, align 1
18+
%tmp39 = sext i8 %tmp38 to i32
19+
ret i32 %tmp39
20+
}

0 commit comments

Comments
 (0)
Please sign in to comment.