1 //===-- XCoreInstrInfo.cpp - XCore Instruction Information ----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains the XCore implementation of the TargetInstrInfo class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "XCoreInstrInfo.h" 15 #include "XCore.h" 16 #include "XCoreMachineFunctionInfo.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/CodeGen/MachineConstantPool.h" 19 #include "llvm/CodeGen/MachineFrameInfo.h" 20 #include "llvm/CodeGen/MachineInstrBuilder.h" 21 #include "llvm/CodeGen/MachineMemOperand.h" 22 #include "llvm/IR/Constants.h" 23 #include "llvm/IR/Function.h" 24 #include "llvm/MC/MCContext.h" 25 #include "llvm/Support/Debug.h" 26 #include "llvm/Support/ErrorHandling.h" 27 #include "llvm/Support/TargetRegistry.h" 28 29 using namespace llvm; 30 31 #define GET_INSTRINFO_CTOR_DTOR 32 #include "XCoreGenInstrInfo.inc" 33 34 namespace llvm { 35 namespace XCore { 36 37 // XCore Condition Codes 38 enum CondCode { 39 COND_TRUE, 40 COND_FALSE, 41 COND_INVALID 42 }; 43 } 44 } 45 46 // Pin the vtable to this file. 47 void XCoreInstrInfo::anchor() {} 48 49 XCoreInstrInfo::XCoreInstrInfo() 50 : XCoreGenInstrInfo(XCore::ADJCALLSTACKDOWN, XCore::ADJCALLSTACKUP), 51 RI() { 52 } 53 54 static bool isZeroImm(const MachineOperand &op) { 55 return op.isImm() && op.getImm() == 0; 56 } 57 58 /// isLoadFromStackSlot - If the specified machine instruction is a direct 59 /// load from a stack slot, return the virtual or physical register number of 60 /// the destination along with the FrameIndex of the loaded stack slot. If 61 /// not, return 0. This predicate must return 0 if the instruction has 62 /// any side effects other than loading from the stack slot. 63 unsigned 64 XCoreInstrInfo::isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const{ 65 int Opcode = MI->getOpcode(); 66 if (Opcode == XCore::LDWFI) 67 { 68 if ((MI->getOperand(1).isFI()) && // is a stack slot 69 (MI->getOperand(2).isImm()) && // the imm is zero 70 (isZeroImm(MI->getOperand(2)))) 71 { 72 FrameIndex = MI->getOperand(1).getIndex(); 73 return MI->getOperand(0).getReg(); 74 } 75 } 76 return 0; 77 } 78 79 /// isStoreToStackSlot - If the specified machine instruction is a direct 80 /// store to a stack slot, return the virtual or physical register number of 81 /// the source reg along with the FrameIndex of the loaded stack slot. If 82 /// not, return 0. This predicate must return 0 if the instruction has 83 /// any side effects other than storing to the stack slot. 84 unsigned 85 XCoreInstrInfo::isStoreToStackSlot(const MachineInstr *MI, 86 int &FrameIndex) const { 87 int Opcode = MI->getOpcode(); 88 if (Opcode == XCore::STWFI) 89 { 90 if ((MI->getOperand(1).isFI()) && // is a stack slot 91 (MI->getOperand(2).isImm()) && // the imm is zero 92 (isZeroImm(MI->getOperand(2)))) 93 { 94 FrameIndex = MI->getOperand(1).getIndex(); 95 return MI->getOperand(0).getReg(); 96 } 97 } 98 return 0; 99 } 100 101 //===----------------------------------------------------------------------===// 102 // Branch Analysis 103 //===----------------------------------------------------------------------===// 104 105 static inline bool IsBRU(unsigned BrOpc) { 106 return BrOpc == XCore::BRFU_u6 107 || BrOpc == XCore::BRFU_lu6 108 || BrOpc == XCore::BRBU_u6 109 || BrOpc == XCore::BRBU_lu6; 110 } 111 112 static inline bool IsBRT(unsigned BrOpc) { 113 return BrOpc == XCore::BRFT_ru6 114 || BrOpc == XCore::BRFT_lru6 115 || BrOpc == XCore::BRBT_ru6 116 || BrOpc == XCore::BRBT_lru6; 117 } 118 119 static inline bool IsBRF(unsigned BrOpc) { 120 return BrOpc == XCore::BRFF_ru6 121 || BrOpc == XCore::BRFF_lru6 122 || BrOpc == XCore::BRBF_ru6 123 || BrOpc == XCore::BRBF_lru6; 124 } 125 126 static inline bool IsCondBranch(unsigned BrOpc) { 127 return IsBRF(BrOpc) || IsBRT(BrOpc); 128 } 129 130 static inline bool IsBR_JT(unsigned BrOpc) { 131 return BrOpc == XCore::BR_JT 132 || BrOpc == XCore::BR_JT32; 133 } 134 135 /// GetCondFromBranchOpc - Return the XCore CC that matches 136 /// the correspondent Branch instruction opcode. 137 static XCore::CondCode GetCondFromBranchOpc(unsigned BrOpc) 138 { 139 if (IsBRT(BrOpc)) { 140 return XCore::COND_TRUE; 141 } else if (IsBRF(BrOpc)) { 142 return XCore::COND_FALSE; 143 } else { 144 return XCore::COND_INVALID; 145 } 146 } 147 148 /// GetCondBranchFromCond - Return the Branch instruction 149 /// opcode that matches the cc. 150 static inline unsigned GetCondBranchFromCond(XCore::CondCode CC) 151 { 152 switch (CC) { 153 default: llvm_unreachable("Illegal condition code!"); 154 case XCore::COND_TRUE : return XCore::BRFT_lru6; 155 case XCore::COND_FALSE : return XCore::BRFF_lru6; 156 } 157 } 158 159 /// GetOppositeBranchCondition - Return the inverse of the specified 160 /// condition, e.g. turning COND_E to COND_NE. 161 static inline XCore::CondCode GetOppositeBranchCondition(XCore::CondCode CC) 162 { 163 switch (CC) { 164 default: llvm_unreachable("Illegal condition code!"); 165 case XCore::COND_TRUE : return XCore::COND_FALSE; 166 case XCore::COND_FALSE : return XCore::COND_TRUE; 167 } 168 } 169 170 /// AnalyzeBranch - Analyze the branching code at the end of MBB, returning 171 /// true if it cannot be understood (e.g. it's a switch dispatch or isn't 172 /// implemented for a target). Upon success, this returns false and returns 173 /// with the following information in various cases: 174 /// 175 /// 1. If this block ends with no branches (it just falls through to its succ) 176 /// just return false, leaving TBB/FBB null. 177 /// 2. If this block ends with only an unconditional branch, it sets TBB to be 178 /// the destination block. 179 /// 3. If this block ends with an conditional branch and it falls through to 180 /// an successor block, it sets TBB to be the branch destination block and a 181 /// list of operands that evaluate the condition. These 182 /// operands can be passed to other TargetInstrInfo methods to create new 183 /// branches. 184 /// 4. If this block ends with an conditional branch and an unconditional 185 /// block, it returns the 'true' destination in TBB, the 'false' destination 186 /// in FBB, and a list of operands that evaluate the condition. These 187 /// operands can be passed to other TargetInstrInfo methods to create new 188 /// branches. 189 /// 190 /// Note that RemoveBranch and InsertBranch must be implemented to support 191 /// cases where this method returns success. 192 /// 193 bool 194 XCoreInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, 195 MachineBasicBlock *&FBB, 196 SmallVectorImpl<MachineOperand> &Cond, 197 bool AllowModify) const { 198 // If the block has no terminators, it just falls into the block after it. 199 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); 200 if (I == MBB.end()) 201 return false; 202 203 if (!isUnpredicatedTerminator(*I)) 204 return false; 205 206 // Get the last instruction in the block. 207 MachineInstr *LastInst = I; 208 209 // If there is only one terminator instruction, process it. 210 if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) { 211 if (IsBRU(LastInst->getOpcode())) { 212 TBB = LastInst->getOperand(0).getMBB(); 213 return false; 214 } 215 216 XCore::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode()); 217 if (BranchCode == XCore::COND_INVALID) 218 return true; // Can't handle indirect branch. 219 220 // Conditional branch 221 // Block ends with fall-through condbranch. 222 223 TBB = LastInst->getOperand(1).getMBB(); 224 Cond.push_back(MachineOperand::CreateImm(BranchCode)); 225 Cond.push_back(LastInst->getOperand(0)); 226 return false; 227 } 228 229 // Get the instruction before it if it's a terminator. 230 MachineInstr *SecondLastInst = I; 231 232 // If there are three terminators, we don't know what sort of block this is. 233 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(*--I)) 234 return true; 235 236 unsigned SecondLastOpc = SecondLastInst->getOpcode(); 237 XCore::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc); 238 239 // If the block ends with conditional branch followed by unconditional, 240 // handle it. 241 if (BranchCode != XCore::COND_INVALID 242 && IsBRU(LastInst->getOpcode())) { 243 244 TBB = SecondLastInst->getOperand(1).getMBB(); 245 Cond.push_back(MachineOperand::CreateImm(BranchCode)); 246 Cond.push_back(SecondLastInst->getOperand(0)); 247 248 FBB = LastInst->getOperand(0).getMBB(); 249 return false; 250 } 251 252 // If the block ends with two unconditional branches, handle it. The second 253 // one is not executed, so remove it. 254 if (IsBRU(SecondLastInst->getOpcode()) && 255 IsBRU(LastInst->getOpcode())) { 256 TBB = SecondLastInst->getOperand(0).getMBB(); 257 I = LastInst; 258 if (AllowModify) 259 I->eraseFromParent(); 260 return false; 261 } 262 263 // Likewise if it ends with a branch table followed by an unconditional branch. 264 if (IsBR_JT(SecondLastInst->getOpcode()) && IsBRU(LastInst->getOpcode())) { 265 I = LastInst; 266 if (AllowModify) 267 I->eraseFromParent(); 268 return true; 269 } 270 271 // Otherwise, can't handle this. 272 return true; 273 } 274 275 unsigned 276 XCoreInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB, 277 MachineBasicBlock *FBB, 278 ArrayRef<MachineOperand> Cond, 279 DebugLoc DL)const{ 280 // Shouldn't be a fall through. 281 assert(TBB && "InsertBranch must not be told to insert a fallthrough"); 282 assert((Cond.size() == 2 || Cond.size() == 0) && 283 "Unexpected number of components!"); 284 285 if (!FBB) { // One way branch. 286 if (Cond.empty()) { 287 // Unconditional branch 288 BuildMI(&MBB, DL, get(XCore::BRFU_lu6)).addMBB(TBB); 289 } else { 290 // Conditional branch. 291 unsigned Opc = GetCondBranchFromCond((XCore::CondCode)Cond[0].getImm()); 292 BuildMI(&MBB, DL, get(Opc)).addReg(Cond[1].getReg()) 293 .addMBB(TBB); 294 } 295 return 1; 296 } 297 298 // Two-way Conditional branch. 299 assert(Cond.size() == 2 && "Unexpected number of components!"); 300 unsigned Opc = GetCondBranchFromCond((XCore::CondCode)Cond[0].getImm()); 301 BuildMI(&MBB, DL, get(Opc)).addReg(Cond[1].getReg()) 302 .addMBB(TBB); 303 BuildMI(&MBB, DL, get(XCore::BRFU_lu6)).addMBB(FBB); 304 return 2; 305 } 306 307 unsigned 308 XCoreInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const { 309 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); 310 if (I == MBB.end()) 311 return 0; 312 313 if (!IsBRU(I->getOpcode()) && !IsCondBranch(I->getOpcode())) 314 return 0; 315 316 // Remove the branch. 317 I->eraseFromParent(); 318 319 I = MBB.end(); 320 321 if (I == MBB.begin()) return 1; 322 --I; 323 if (!IsCondBranch(I->getOpcode())) 324 return 1; 325 326 // Remove the branch. 327 I->eraseFromParent(); 328 return 2; 329 } 330 331 void XCoreInstrInfo::copyPhysReg(MachineBasicBlock &MBB, 332 MachineBasicBlock::iterator I, DebugLoc DL, 333 unsigned DestReg, unsigned SrcReg, 334 bool KillSrc) const { 335 bool GRDest = XCore::GRRegsRegClass.contains(DestReg); 336 bool GRSrc = XCore::GRRegsRegClass.contains(SrcReg); 337 338 if (GRDest && GRSrc) { 339 BuildMI(MBB, I, DL, get(XCore::ADD_2rus), DestReg) 340 .addReg(SrcReg, getKillRegState(KillSrc)) 341 .addImm(0); 342 return; 343 } 344 345 if (GRDest && SrcReg == XCore::SP) { 346 BuildMI(MBB, I, DL, get(XCore::LDAWSP_ru6), DestReg).addImm(0); 347 return; 348 } 349 350 if (DestReg == XCore::SP && GRSrc) { 351 BuildMI(MBB, I, DL, get(XCore::SETSP_1r)) 352 .addReg(SrcReg, getKillRegState(KillSrc)); 353 return; 354 } 355 llvm_unreachable("Impossible reg-to-reg copy"); 356 } 357 358 void XCoreInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, 359 MachineBasicBlock::iterator I, 360 unsigned SrcReg, bool isKill, 361 int FrameIndex, 362 const TargetRegisterClass *RC, 363 const TargetRegisterInfo *TRI) const 364 { 365 DebugLoc DL; 366 if (I != MBB.end() && !I->isDebugValue()) 367 DL = I->getDebugLoc(); 368 MachineFunction *MF = MBB.getParent(); 369 const MachineFrameInfo &MFI = *MF->getFrameInfo(); 370 MachineMemOperand *MMO = MF->getMachineMemOperand( 371 MachinePointerInfo::getFixedStack(*MF, FrameIndex), 372 MachineMemOperand::MOStore, MFI.getObjectSize(FrameIndex), 373 MFI.getObjectAlignment(FrameIndex)); 374 BuildMI(MBB, I, DL, get(XCore::STWFI)) 375 .addReg(SrcReg, getKillRegState(isKill)) 376 .addFrameIndex(FrameIndex) 377 .addImm(0) 378 .addMemOperand(MMO); 379 } 380 381 void XCoreInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, 382 MachineBasicBlock::iterator I, 383 unsigned DestReg, int FrameIndex, 384 const TargetRegisterClass *RC, 385 const TargetRegisterInfo *TRI) const 386 { 387 DebugLoc DL; 388 if (I != MBB.end() && !I->isDebugValue()) 389 DL = I->getDebugLoc(); 390 MachineFunction *MF = MBB.getParent(); 391 const MachineFrameInfo &MFI = *MF->getFrameInfo(); 392 MachineMemOperand *MMO = MF->getMachineMemOperand( 393 MachinePointerInfo::getFixedStack(*MF, FrameIndex), 394 MachineMemOperand::MOLoad, MFI.getObjectSize(FrameIndex), 395 MFI.getObjectAlignment(FrameIndex)); 396 BuildMI(MBB, I, DL, get(XCore::LDWFI), DestReg) 397 .addFrameIndex(FrameIndex) 398 .addImm(0) 399 .addMemOperand(MMO); 400 } 401 402 /// ReverseBranchCondition - Return the inverse opcode of the 403 /// specified Branch instruction. 404 bool XCoreInstrInfo:: 405 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const { 406 assert((Cond.size() == 2) && 407 "Invalid XCore branch condition!"); 408 Cond[0].setImm(GetOppositeBranchCondition((XCore::CondCode)Cond[0].getImm())); 409 return false; 410 } 411 412 static inline bool isImmU6(unsigned val) { 413 return val < (1 << 6); 414 } 415 416 static inline bool isImmU16(unsigned val) { 417 return val < (1 << 16); 418 } 419 420 static bool isImmMskBitp(unsigned val) { 421 if (!isMask_32(val)) { 422 return false; 423 } 424 int N = Log2_32(val) + 1; 425 return (N >= 1 && N <= 8) || N == 16 || N == 24 || N == 32; 426 } 427 428 MachineBasicBlock::iterator XCoreInstrInfo::loadImmediate( 429 MachineBasicBlock &MBB, 430 MachineBasicBlock::iterator MI, 431 unsigned Reg, uint64_t Value) const { 432 DebugLoc dl; 433 if (MI != MBB.end() && !MI->isDebugValue()) 434 dl = MI->getDebugLoc(); 435 if (isImmMskBitp(Value)) { 436 int N = Log2_32(Value) + 1; 437 return BuildMI(MBB, MI, dl, get(XCore::MKMSK_rus), Reg) 438 .addImm(N) 439 .getInstr(); 440 } 441 if (isImmU16(Value)) { 442 int Opcode = isImmU6(Value) ? XCore::LDC_ru6 : XCore::LDC_lru6; 443 return BuildMI(MBB, MI, dl, get(Opcode), Reg).addImm(Value).getInstr(); 444 } 445 MachineConstantPool *ConstantPool = MBB.getParent()->getConstantPool(); 446 const Constant *C = ConstantInt::get( 447 Type::getInt32Ty(MBB.getParent()->getFunction()->getContext()), Value); 448 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4); 449 return BuildMI(MBB, MI, dl, get(XCore::LDWCP_lru6), Reg) 450 .addConstantPoolIndex(Idx) 451 .getInstr(); 452 } 453