All store instructions really want 'rd' in the first field.
[oota-llvm.git] / lib / Target / X86 / InstSelectSimple.cpp
index c62e68da270ddcc4a9a41e5a494750de3422823f..b19c5c208ebf74384e78c34149cb2964b6057b87 100644 (file)
@@ -27,8 +27,7 @@
 /// have a destination register in mind.
 inline static MachineInstrBuilder BMI(MachineBasicBlock *MBB,
                                       MachineBasicBlock::iterator &I,
-                                      MachineOpCode Opcode,
-                                      unsigned NumOperands,
+                                      int Opcode, unsigned NumOperands,
                                       unsigned DestReg) {
   assert(I >= MBB->begin() && I <= MBB->end() && "Bad iterator!");
   MachineInstr *MI = new MachineInstr(Opcode, NumOperands+1, true, true);
@@ -40,8 +39,7 @@ inline static MachineInstrBuilder BMI(MachineBasicBlock *MBB,
 /// instruction at as well as a basic block.
 inline static MachineInstrBuilder BMI(MachineBasicBlock *MBB,
                                       MachineBasicBlock::iterator &I,
-                                      MachineOpCode Opcode,
-                                      unsigned NumOperands) {
+                                      int Opcode, unsigned NumOperands) {
   assert(I >= MBB->begin() && I <= MBB->end() && "Bad iterator!");
   MachineInstr *MI = new MachineInstr(Opcode, NumOperands, true, true);
   I = MBB->insert(I, MI)+1;
@@ -1552,10 +1550,28 @@ void ISel::emitCastOperation(MachineBasicBlock *BB,
   // Implement casts to bool by using compare on the operand followed by set if
   // not zero on the result.
   if (DestTy == Type::BoolTy) {
-    if (SrcClass == cFP || SrcClass == cLong)
-      abort();  // FIXME: implement cast (long & FP) to bool
-    
-    BMI(BB, IP, X86::CMPri8, 2).addReg(SrcReg).addZImm(0);
+    switch (SrcClass) {
+    case cByte:
+      BMI(BB, IP, X86::TESTrr8, 2).addReg(SrcReg).addReg(SrcReg);
+      break;
+    case cShort:
+      BMI(BB, IP, X86::TESTrr16, 2).addReg(SrcReg).addReg(SrcReg);
+      break;
+    case cInt:
+      BMI(BB, IP, X86::TESTrr32, 2).addReg(SrcReg).addReg(SrcReg);
+      break;
+    case cLong: {
+      unsigned TmpReg = makeAnotherReg(Type::IntTy);
+      BMI(BB, IP, X86::ORrr32, 2, TmpReg).addReg(SrcReg).addReg(SrcReg+1);
+      break;
+    }
+    case cFP:
+      assert(0 && "FIXME: implement cast FP to bool");
+      abort();
+    }
+
+    // If the zero flag is not set, then the value is true, set the byte to
+    // true.
     BMI(BB, IP, X86::SETNEr, 1, DestReg);
     return;
   }