Chris Lattner | 6b94453 | 2002-10-28 01:16:38 +0000 | [diff] [blame] | 1 | //===-- MachineFunction.cpp -----------------------------------------------===// |
Alkis Evlogimenos | 76d9dac | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Alkis Evlogimenos | 76d9dac | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 76d9dac | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 6b94453 | 2002-10-28 01:16:38 +0000 | [diff] [blame] | 10 | // Collect native machine code information for a function. This allows |
| 11 | // target-specific information about the generated code to be stored with each |
| 12 | // function. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 15 | |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 16 | #include "llvm/DerivedTypes.h" |
Bill Wendling | 20c568f | 2009-06-30 22:38:32 +0000 | [diff] [blame] | 17 | #include "llvm/Function.h" |
| 18 | #include "llvm/Instructions.h" |
Bill Wendling | 20c568f | 2009-06-30 22:38:32 +0000 | [diff] [blame] | 19 | #include "llvm/Config/config.h" |
Chris Lattner | 4d149cd | 2003-01-13 00:23:03 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineConstantPool.h" |
David Greene | 098612b | 2009-08-19 22:16:11 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 23 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 24 | #include "llvm/CodeGen/MachineInstr.h" |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chris Lattner | 16c45e9 | 2003-12-20 10:20:58 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/Passes.h" |
Chris Lattner | beeb93e | 2010-01-26 05:58:28 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCAsmInfo.h" |
| 29 | #include "llvm/MC/MCContext.h" |
Devang Patel | c99fd87 | 2010-01-19 06:09:04 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/DebugInfo.h" |
David Greene | dc55481 | 2010-01-04 23:39:17 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
Owen Anderson | 07000c6 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetData.h" |
Bill Wendling | 20c568f | 2009-06-30 22:38:32 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetLowering.h" |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 8bd66e6 | 2002-12-28 21:00:25 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetFrameInfo.h" |
Chris Lattner | beeb93e | 2010-01-26 05:58:28 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/SmallString.h" |
| 37 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | f28bbda | 2006-10-03 20:19:23 +0000 | [diff] [blame] | 38 | #include "llvm/Support/GraphWriter.h" |
Chris Lattner | 944fac7 | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 39 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 07f32d4 | 2003-12-20 09:17:07 +0000 | [diff] [blame] | 40 | using namespace llvm; |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 227c3d3 | 2002-10-28 01:12:41 +0000 | [diff] [blame] | 42 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 43 | struct Printer : public MachineFunctionPass { |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 44 | static char ID; |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 45 | |
Chris Lattner | cf143a4 | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 46 | raw_ostream &OS; |
Chris Lattner | d4baf0f | 2004-02-01 05:25:07 +0000 | [diff] [blame] | 47 | const std::string Banner; |
Brian Gaeke | 09caa37 | 2004-01-30 21:53:46 +0000 | [diff] [blame] | 48 | |
Chris Lattner | cf143a4 | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 49 | Printer(raw_ostream &os, const std::string &banner) |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 50 | : MachineFunctionPass(&ID), OS(os), Banner(banner) {} |
Brian Gaeke | 09caa37 | 2004-01-30 21:53:46 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 52 | const char *getPassName() const { return "MachineFunction Printer"; } |
| 53 | |
| 54 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 55 | AU.setPreservesAll(); |
Dan Gohman | ad2afc2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 56 | MachineFunctionPass::getAnalysisUsage(AU); |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Chris Lattner | 16c45e9 | 2003-12-20 10:20:58 +0000 | [diff] [blame] | 59 | bool runOnMachineFunction(MachineFunction &MF) { |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 60 | OS << "# " << Banner << ":\n"; |
Chris Lattner | cf143a4 | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 61 | MF.print(OS); |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | }; |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 65 | char Printer::ID = 0; |
Chris Lattner | 227c3d3 | 2002-10-28 01:12:41 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Daniel Dunbar | 275872e | 2009-08-03 01:02:24 +0000 | [diff] [blame] | 68 | /// Returns a newly-created MachineFunction Printer pass. The default banner is |
| 69 | /// empty. |
Brian Gaeke | 09caa37 | 2004-01-30 21:53:46 +0000 | [diff] [blame] | 70 | /// |
Chris Lattner | cf143a4 | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 71 | FunctionPass *llvm::createMachineFunctionPrinterPass(raw_ostream &OS, |
Chris Lattner | ce9c41e | 2005-01-23 22:13:58 +0000 | [diff] [blame] | 72 | const std::string &Banner){ |
Brian Gaeke | 09caa37 | 2004-01-30 21:53:46 +0000 | [diff] [blame] | 73 | return new Printer(OS, Banner); |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Chris Lattner | b84822f | 2010-01-26 04:35:26 +0000 | [diff] [blame] | 76 | //===----------------------------------------------------------------------===// |
Chris Lattner | 227c3d3 | 2002-10-28 01:12:41 +0000 | [diff] [blame] | 77 | // MachineFunction implementation |
Chris Lattner | b84822f | 2010-01-26 04:35:26 +0000 | [diff] [blame] | 78 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9d5d759 | 2005-01-29 18:41:25 +0000 | [diff] [blame] | 79 | |
Chris Lattner | a70e2e3 | 2009-09-15 22:44:26 +0000 | [diff] [blame] | 80 | // Out of line virtual method. |
| 81 | MachineFunctionInfo::~MachineFunctionInfo() {} |
| 82 | |
Dan Gohman | fed90b6 | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 83 | void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) { |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 84 | MBB->getParent()->DeleteMachineBasicBlock(MBB); |
Tanya Lattner | 792699c | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 85 | } |
Chris Lattner | 227c3d3 | 2002-10-28 01:12:41 +0000 | [diff] [blame] | 86 | |
Chris Lattner | b84822f | 2010-01-26 04:35:26 +0000 | [diff] [blame] | 87 | MachineFunction::MachineFunction(Function *F, const TargetMachine &TM, |
Chris Lattner | 11d53c1 | 2010-03-13 20:55:24 +0000 | [diff] [blame] | 88 | unsigned FunctionNum, MCContext &ctx) |
| 89 | : Fn(F), Target(TM), Ctx(ctx) { |
Matthijs Kooijman | e2b997b | 2008-10-13 12:37:16 +0000 | [diff] [blame] | 90 | if (TM.getRegisterInfo()) |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 91 | RegInfo = new (Allocator) MachineRegisterInfo(*TM.getRegisterInfo()); |
Matthijs Kooijman | e2b997b | 2008-10-13 12:37:16 +0000 | [diff] [blame] | 92 | else |
| 93 | RegInfo = 0; |
Chris Lattner | ad82816 | 2004-08-16 22:36:34 +0000 | [diff] [blame] | 94 | MFInfo = 0; |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 95 | FrameInfo = new (Allocator) MachineFrameInfo(*TM.getFrameInfo()); |
Charles Davis | 5dfa267 | 2010-02-19 18:17:13 +0000 | [diff] [blame] | 96 | if (Fn->hasFnAttr(Attribute::StackAlignment)) |
| 97 | FrameInfo->setMaxAlignment(Attribute::getStackAlignmentFromAttrs( |
| 98 | Fn->getAttributes().getFnAttributes())); |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 99 | ConstantPool = new (Allocator) MachineConstantPool(TM.getTargetData()); |
Bill Wendling | 20c568f | 2009-06-30 22:38:32 +0000 | [diff] [blame] | 100 | Alignment = TM.getTargetLowering()->getFunctionAlignment(F); |
Chris Lattner | b84822f | 2010-01-26 04:35:26 +0000 | [diff] [blame] | 101 | FunctionNumber = FunctionNum; |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 102 | JumpTableInfo = 0; |
Chris Lattner | 831fdcf | 2002-12-25 05:03:22 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Alkis Evlogimenos | 76d9dac | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 105 | MachineFunction::~MachineFunction() { |
Chris Lattner | 4b9a400 | 2004-07-01 06:29:07 +0000 | [diff] [blame] | 106 | BasicBlocks.clear(); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 107 | InstructionRecycler.clear(Allocator); |
| 108 | BasicBlockRecycler.clear(Allocator); |
Bill Wendling | dd37b36 | 2009-06-25 00:32:48 +0000 | [diff] [blame] | 109 | if (RegInfo) { |
| 110 | RegInfo->~MachineRegisterInfo(); |
| 111 | Allocator.Deallocate(RegInfo); |
| 112 | } |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 113 | if (MFInfo) { |
Bill Wendling | dd37b36 | 2009-06-25 00:32:48 +0000 | [diff] [blame] | 114 | MFInfo->~MachineFunctionInfo(); |
| 115 | Allocator.Deallocate(MFInfo); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 116 | } |
| 117 | FrameInfo->~MachineFrameInfo(); Allocator.Deallocate(FrameInfo); |
| 118 | ConstantPool->~MachineConstantPool(); Allocator.Deallocate(ConstantPool); |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 119 | |
| 120 | if (JumpTableInfo) { |
| 121 | JumpTableInfo->~MachineJumpTableInfo(); |
| 122 | Allocator.Deallocate(JumpTableInfo); |
| 123 | } |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 126 | /// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it |
| 127 | /// does already exist, allocate one. |
| 128 | MachineJumpTableInfo *MachineFunction:: |
| 129 | getOrCreateJumpTableInfo(unsigned EntryKind) { |
| 130 | if (JumpTableInfo) return JumpTableInfo; |
| 131 | |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 132 | JumpTableInfo = new (Allocator) |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 133 | MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind); |
| 134 | return JumpTableInfo; |
| 135 | } |
Chris Lattner | e70cab0 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 136 | |
| 137 | /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and |
| 138 | /// recomputes them. This guarantees that the MBB numbers are sequential, |
| 139 | /// dense, and match the ordering of the blocks within the function. If a |
| 140 | /// specific MachineBasicBlock is specified, only that block and those after |
| 141 | /// it are renumbered. |
| 142 | void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) { |
| 143 | if (empty()) { MBBNumbering.clear(); return; } |
| 144 | MachineFunction::iterator MBBI, E = end(); |
| 145 | if (MBB == 0) |
| 146 | MBBI = begin(); |
| 147 | else |
| 148 | MBBI = MBB; |
| 149 | |
| 150 | // Figure out the block number this should have. |
| 151 | unsigned BlockNo = 0; |
Chris Lattner | f28bbda | 2006-10-03 20:19:23 +0000 | [diff] [blame] | 152 | if (MBBI != begin()) |
| 153 | BlockNo = prior(MBBI)->getNumber()+1; |
Chris Lattner | e70cab0 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 154 | |
| 155 | for (; MBBI != E; ++MBBI, ++BlockNo) { |
| 156 | if (MBBI->getNumber() != (int)BlockNo) { |
| 157 | // Remove use of the old number. |
| 158 | if (MBBI->getNumber() != -1) { |
| 159 | assert(MBBNumbering[MBBI->getNumber()] == &*MBBI && |
| 160 | "MBB number mismatch!"); |
| 161 | MBBNumbering[MBBI->getNumber()] = 0; |
| 162 | } |
| 163 | |
| 164 | // If BlockNo is already taken, set that block's number to -1. |
| 165 | if (MBBNumbering[BlockNo]) |
| 166 | MBBNumbering[BlockNo]->setNumber(-1); |
| 167 | |
| 168 | MBBNumbering[BlockNo] = MBBI; |
| 169 | MBBI->setNumber(BlockNo); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // Okay, all the blocks are renumbered. If we have compactified the block |
| 174 | // numbering, shrink MBBNumbering now. |
| 175 | assert(BlockNo <= MBBNumbering.size() && "Mismatch!"); |
| 176 | MBBNumbering.resize(BlockNo); |
| 177 | } |
| 178 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 179 | /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead |
| 180 | /// of `new MachineInstr'. |
| 181 | /// |
| 182 | MachineInstr * |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 183 | MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID, |
| 184 | DebugLoc DL, bool NoImp) { |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 185 | return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) |
Bill Wendling | 9bc96a5 | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 186 | MachineInstr(TID, DL, NoImp); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 187 | } |
Chris Lattner | e70cab0 | 2006-10-03 19:18:57 +0000 | [diff] [blame] | 188 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 189 | /// CloneMachineInstr - Create a new MachineInstr which is a copy of the |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 190 | /// 'Orig' instruction, identical in all ways except the instruction |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 191 | /// has no parent, prev, or next. |
| 192 | /// |
| 193 | MachineInstr * |
| 194 | MachineFunction::CloneMachineInstr(const MachineInstr *Orig) { |
| 195 | return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) |
| 196 | MachineInstr(*this, *Orig); |
| 197 | } |
| 198 | |
| 199 | /// DeleteMachineInstr - Delete the given MachineInstr. |
| 200 | /// |
| 201 | void |
| 202 | MachineFunction::DeleteMachineInstr(MachineInstr *MI) { |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 203 | MI->~MachineInstr(); |
| 204 | InstructionRecycler.Deallocate(Allocator, MI); |
| 205 | } |
| 206 | |
| 207 | /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this |
| 208 | /// instead of `new MachineBasicBlock'. |
| 209 | /// |
| 210 | MachineBasicBlock * |
| 211 | MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) { |
| 212 | return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator)) |
| 213 | MachineBasicBlock(*this, bb); |
| 214 | } |
| 215 | |
| 216 | /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock. |
| 217 | /// |
| 218 | void |
| 219 | MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) { |
| 220 | assert(MBB->getParent() == this && "MBB parent mismatch!"); |
| 221 | MBB->~MachineBasicBlock(); |
| 222 | BasicBlockRecycler.Deallocate(Allocator, MBB); |
| 223 | } |
| 224 | |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 225 | MachineMemOperand * |
| 226 | MachineFunction::getMachineMemOperand(const Value *v, unsigned f, |
| 227 | int64_t o, uint64_t s, |
| 228 | unsigned base_alignment) { |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 229 | return new (Allocator) MachineMemOperand(v, f, o, s, base_alignment); |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | MachineMemOperand * |
| 233 | MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, |
| 234 | int64_t Offset, uint64_t Size) { |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 235 | return new (Allocator) |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 236 | MachineMemOperand(MMO->getValue(), MMO->getFlags(), |
| 237 | int64_t(uint64_t(MMO->getOffset()) + |
| 238 | uint64_t(Offset)), |
| 239 | Size, MMO->getBaseAlignment()); |
| 240 | } |
| 241 | |
| 242 | MachineInstr::mmo_iterator |
| 243 | MachineFunction::allocateMemRefsArray(unsigned long Num) { |
| 244 | return Allocator.Allocate<MachineMemOperand *>(Num); |
| 245 | } |
| 246 | |
Dan Gohman | 91e69c3 | 2009-10-09 18:10:05 +0000 | [diff] [blame] | 247 | std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator> |
| 248 | MachineFunction::extractLoadMemRefs(MachineInstr::mmo_iterator Begin, |
| 249 | MachineInstr::mmo_iterator End) { |
| 250 | // Count the number of load mem refs. |
| 251 | unsigned Num = 0; |
| 252 | for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) |
| 253 | if ((*I)->isLoad()) |
| 254 | ++Num; |
| 255 | |
| 256 | // Allocate a new array and populate it with the load information. |
| 257 | MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num); |
| 258 | unsigned Index = 0; |
| 259 | for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) { |
| 260 | if ((*I)->isLoad()) { |
| 261 | if (!(*I)->isStore()) |
| 262 | // Reuse the MMO. |
| 263 | Result[Index] = *I; |
| 264 | else { |
| 265 | // Clone the MMO and unset the store flag. |
| 266 | MachineMemOperand *JustLoad = |
| 267 | getMachineMemOperand((*I)->getValue(), |
| 268 | (*I)->getFlags() & ~MachineMemOperand::MOStore, |
| 269 | (*I)->getOffset(), (*I)->getSize(), |
| 270 | (*I)->getBaseAlignment()); |
| 271 | Result[Index] = JustLoad; |
| 272 | } |
| 273 | ++Index; |
| 274 | } |
| 275 | } |
| 276 | return std::make_pair(Result, Result + Num); |
| 277 | } |
| 278 | |
| 279 | std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator> |
| 280 | MachineFunction::extractStoreMemRefs(MachineInstr::mmo_iterator Begin, |
| 281 | MachineInstr::mmo_iterator End) { |
| 282 | // Count the number of load mem refs. |
| 283 | unsigned Num = 0; |
| 284 | for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) |
| 285 | if ((*I)->isStore()) |
| 286 | ++Num; |
| 287 | |
| 288 | // Allocate a new array and populate it with the store information. |
| 289 | MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num); |
| 290 | unsigned Index = 0; |
| 291 | for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) { |
| 292 | if ((*I)->isStore()) { |
| 293 | if (!(*I)->isLoad()) |
| 294 | // Reuse the MMO. |
| 295 | Result[Index] = *I; |
| 296 | else { |
| 297 | // Clone the MMO and unset the load flag. |
| 298 | MachineMemOperand *JustStore = |
| 299 | getMachineMemOperand((*I)->getValue(), |
| 300 | (*I)->getFlags() & ~MachineMemOperand::MOLoad, |
| 301 | (*I)->getOffset(), (*I)->getSize(), |
| 302 | (*I)->getBaseAlignment()); |
| 303 | Result[Index] = JustStore; |
| 304 | } |
| 305 | ++Index; |
| 306 | } |
| 307 | } |
| 308 | return std::make_pair(Result, Result + Num); |
| 309 | } |
| 310 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 311 | void MachineFunction::dump() const { |
David Greene | dc55481 | 2010-01-04 23:39:17 +0000 | [diff] [blame] | 312 | print(dbgs()); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 313 | } |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 314 | |
Chris Lattner | d74c556 | 2009-08-23 01:12:47 +0000 | [diff] [blame] | 315 | void MachineFunction::print(raw_ostream &OS) const { |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 316 | OS << "# Machine code for function " << Fn->getName() << ":\n"; |
Chris Lattner | 955fad1 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 317 | |
| 318 | // Print Frame Information |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 319 | FrameInfo->print(*this, OS); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 320 | |
| 321 | // Print JumpTable Information |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 322 | if (JumpTableInfo) |
| 323 | JumpTableInfo->print(OS); |
Chris Lattner | 4d149cd | 2003-01-13 00:23:03 +0000 | [diff] [blame] | 324 | |
| 325 | // Print Constant Pool |
Chris Lattner | d74c556 | 2009-08-23 01:12:47 +0000 | [diff] [blame] | 326 | ConstantPool->print(OS); |
Chris Lattner | a1f68ca | 2005-08-31 22:34:59 +0000 | [diff] [blame] | 327 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 328 | const TargetRegisterInfo *TRI = getTarget().getRegisterInfo(); |
Chris Lattner | a1f68ca | 2005-08-31 22:34:59 +0000 | [diff] [blame] | 329 | |
Matthijs Kooijman | e2b997b | 2008-10-13 12:37:16 +0000 | [diff] [blame] | 330 | if (RegInfo && !RegInfo->livein_empty()) { |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 331 | OS << "Function Live Ins: "; |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 332 | for (MachineRegisterInfo::livein_iterator |
| 333 | I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 334 | if (TRI) |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 335 | OS << "%" << TRI->getName(I->first); |
Chris Lattner | a1f68ca | 2005-08-31 22:34:59 +0000 | [diff] [blame] | 336 | else |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 337 | OS << " %physreg" << I->first; |
Chris Lattner | 4e92027 | 2006-05-16 05:55:30 +0000 | [diff] [blame] | 338 | |
| 339 | if (I->second) |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 340 | OS << " in reg%" << I->second; |
| 341 | |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 342 | if (llvm::next(I) != E) |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 343 | OS << ", "; |
Chris Lattner | a1f68ca | 2005-08-31 22:34:59 +0000 | [diff] [blame] | 344 | } |
Chris Lattner | d74c556 | 2009-08-23 01:12:47 +0000 | [diff] [blame] | 345 | OS << '\n'; |
Chris Lattner | a1f68ca | 2005-08-31 22:34:59 +0000 | [diff] [blame] | 346 | } |
Matthijs Kooijman | e2b997b | 2008-10-13 12:37:16 +0000 | [diff] [blame] | 347 | if (RegInfo && !RegInfo->liveout_empty()) { |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 348 | OS << "Function Live Outs: "; |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 349 | for (MachineRegisterInfo::liveout_iterator |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 350 | I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I){ |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 351 | if (TRI) |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 352 | OS << '%' << TRI->getName(*I); |
Chris Lattner | a1f68ca | 2005-08-31 22:34:59 +0000 | [diff] [blame] | 353 | else |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 354 | OS << "%physreg" << *I; |
| 355 | |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 356 | if (llvm::next(I) != E) |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 357 | OS << " "; |
| 358 | } |
Chris Lattner | d74c556 | 2009-08-23 01:12:47 +0000 | [diff] [blame] | 359 | OS << '\n'; |
Chris Lattner | a1f68ca | 2005-08-31 22:34:59 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 362 | for (const_iterator BB = begin(), E = end(); BB != E; ++BB) { |
| 363 | OS << '\n'; |
Chris Lattner | 2d8e3d2 | 2009-08-23 00:47:04 +0000 | [diff] [blame] | 364 | BB->print(OS); |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 365 | } |
Brian Gaeke | 47b7164 | 2004-03-29 21:58:31 +0000 | [diff] [blame] | 366 | |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 367 | OS << "\n# End machine code for function " << Fn->getName() << ".\n\n"; |
Chris Lattner | 1049164 | 2002-10-30 00:48:05 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Alkis Evlogimenos | 71bf404 | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 370 | namespace llvm { |
Alkis Evlogimenos | 76d9dac | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 371 | template<> |
| 372 | struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits { |
Tobias Grosser | a10d598 | 2009-11-30 12:38:13 +0000 | [diff] [blame] | 373 | |
| 374 | DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {} |
| 375 | |
Alkis Evlogimenos | 76d9dac | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 376 | static std::string getGraphName(const MachineFunction *F) { |
Daniel Dunbar | f6ccee5 | 2009-07-24 08:24:36 +0000 | [diff] [blame] | 377 | return "CFG for '" + F->getFunction()->getNameStr() + "' function"; |
Alkis Evlogimenos | 71bf404 | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Tobias Grosser | 56f4ef3 | 2009-11-30 12:38:47 +0000 | [diff] [blame] | 380 | std::string getNodeLabel(const MachineBasicBlock *Node, |
| 381 | const MachineFunction *Graph) { |
| 382 | if (isSimple () && Node->getBasicBlock() && |
Alkis Evlogimenos | 76d9dac | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 383 | !Node->getBasicBlock()->getName().empty()) |
Daniel Dunbar | f6ccee5 | 2009-07-24 08:24:36 +0000 | [diff] [blame] | 384 | return Node->getBasicBlock()->getNameStr() + ":"; |
Alkis Evlogimenos | 71bf404 | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 385 | |
Chris Lattner | cf143a4 | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 386 | std::string OutStr; |
| 387 | { |
| 388 | raw_string_ostream OSS(OutStr); |
| 389 | |
Tobias Grosser | 56f4ef3 | 2009-11-30 12:38:47 +0000 | [diff] [blame] | 390 | if (isSimple()) |
Chris Lattner | cf143a4 | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 391 | OSS << Node->getNumber() << ':'; |
| 392 | else |
| 393 | Node->print(OSS); |
Alkis Evlogimenos | 71bf404 | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 394 | } |
Alkis Evlogimenos | 76d9dac | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 395 | |
Alkis Evlogimenos | 76d9dac | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 396 | if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); |
| 397 | |
| 398 | // Process string output to make it nicer... |
| 399 | for (unsigned i = 0; i != OutStr.length(); ++i) |
| 400 | if (OutStr[i] == '\n') { // Left justify |
| 401 | OutStr[i] = '\\'; |
| 402 | OutStr.insert(OutStr.begin()+i+1, 'l'); |
| 403 | } |
| 404 | return OutStr; |
| 405 | } |
| 406 | }; |
Alkis Evlogimenos | 71bf404 | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | void MachineFunction::viewCFG() const |
| 410 | { |
Jim Laskey | 851a22d | 2005-10-12 12:09:05 +0000 | [diff] [blame] | 411 | #ifndef NDEBUG |
Daniel Dunbar | f6ccee5 | 2009-07-24 08:24:36 +0000 | [diff] [blame] | 412 | ViewGraph(this, "mf" + getFunction()->getNameStr()); |
Reid Spencer | 9d5b532 | 2006-06-27 16:49:46 +0000 | [diff] [blame] | 413 | #else |
Daniel Dunbar | 43ed267 | 2009-08-23 08:50:52 +0000 | [diff] [blame] | 414 | errs() << "SelectionDAG::viewGraph is only available in debug builds on " |
| 415 | << "systems with Graphviz or gv!\n"; |
Reid Spencer | 9d5b532 | 2006-06-27 16:49:46 +0000 | [diff] [blame] | 416 | #endif // NDEBUG |
Alkis Evlogimenos | 71bf404 | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | void MachineFunction::viewCFGOnly() const |
| 420 | { |
Owen Anderson | 8cbc94a | 2009-06-24 17:37:09 +0000 | [diff] [blame] | 421 | #ifndef NDEBUG |
Daniel Dunbar | f6ccee5 | 2009-07-24 08:24:36 +0000 | [diff] [blame] | 422 | ViewGraph(this, "mf" + getFunction()->getNameStr(), true); |
Owen Anderson | 8cbc94a | 2009-06-24 17:37:09 +0000 | [diff] [blame] | 423 | #else |
Daniel Dunbar | 43ed267 | 2009-08-23 08:50:52 +0000 | [diff] [blame] | 424 | errs() << "SelectionDAG::viewGraph is only available in debug builds on " |
| 425 | << "systems with Graphviz or gv!\n"; |
Owen Anderson | 8cbc94a | 2009-06-24 17:37:09 +0000 | [diff] [blame] | 426 | #endif // NDEBUG |
Alkis Evlogimenos | 71bf404 | 2004-07-08 00:47:58 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Bob Wilson | 998e125 | 2009-04-20 18:36:57 +0000 | [diff] [blame] | 429 | /// addLiveIn - Add the specified physical register as a live-in value and |
| 430 | /// create a corresponding virtual register for it. |
| 431 | unsigned MachineFunction::addLiveIn(unsigned PReg, |
| 432 | const TargetRegisterClass *RC) { |
| 433 | assert(RC->contains(PReg) && "Not the correct regclass!"); |
| 434 | unsigned VReg = getRegInfo().createVirtualRegister(RC); |
| 435 | getRegInfo().addLiveIn(PReg, VReg); |
| 436 | return VReg; |
| 437 | } |
| 438 | |
Devang Patel | 6b61f58 | 2010-01-16 06:09:35 +0000 | [diff] [blame] | 439 | /// getDILocation - Get the DILocation for a given DebugLoc object. |
| 440 | DILocation MachineFunction::getDILocation(DebugLoc DL) const { |
Bill Wendling | 44f6ac6 | 2009-02-03 22:55:54 +0000 | [diff] [blame] | 441 | unsigned Idx = DL.getIndex(); |
Bill Wendling | 85e3af9 | 2009-02-03 22:49:58 +0000 | [diff] [blame] | 442 | assert(Idx < DebugLocInfo.DebugLocations.size() && |
| 443 | "Invalid index into debug locations!"); |
Devang Patel | 6b61f58 | 2010-01-16 06:09:35 +0000 | [diff] [blame] | 444 | return DILocation(DebugLocInfo.DebugLocations[Idx]); |
Bill Wendling | 85e3af9 | 2009-02-03 22:49:58 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Chris Lattner | 589c6f6 | 2010-01-26 06:28:43 +0000 | [diff] [blame] | 447 | |
| 448 | /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table. |
| 449 | /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a |
| 450 | /// normal 'L' label is returned. |
| 451 | MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx, |
| 452 | bool isLinkerPrivate) const { |
| 453 | assert(JumpTableInfo && "No jump tables"); |
| 454 | |
Chandler Carruth | de4c080 | 2010-01-27 10:27:10 +0000 | [diff] [blame] | 455 | assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!"); |
Chris Lattner | 589c6f6 | 2010-01-26 06:28:43 +0000 | [diff] [blame] | 456 | const MCAsmInfo &MAI = *getTarget().getMCAsmInfo(); |
| 457 | |
| 458 | const char *Prefix = isLinkerPrivate ? MAI.getLinkerPrivateGlobalPrefix() : |
| 459 | MAI.getPrivateGlobalPrefix(); |
| 460 | SmallString<60> Name; |
| 461 | raw_svector_ostream(Name) |
| 462 | << Prefix << "JTI" << getFunctionNumber() << '_' << JTI; |
Chris Lattner | 98cdab5 | 2010-03-10 02:25:11 +0000 | [diff] [blame] | 463 | if (isLinkerPrivate) |
| 464 | return Ctx.GetOrCreateSymbol(Name.str()); |
| 465 | return Ctx.GetOrCreateTemporarySymbol(Name.str()); |
Chris Lattner | 589c6f6 | 2010-01-26 06:28:43 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | |
Chris Lattner | 955fad1 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 469 | //===----------------------------------------------------------------------===// |
Chris Lattner | eb24db9 | 2002-12-28 21:08:26 +0000 | [diff] [blame] | 470 | // MachineFrameInfo implementation |
Chris Lattner | 955fad1 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 471 | //===----------------------------------------------------------------------===// |
| 472 | |
Chris Lattner | 1612faa | 2008-01-25 07:19:06 +0000 | [diff] [blame] | 473 | /// CreateFixedObject - Create a new object at a fixed location on the stack. |
| 474 | /// All fixed objects should be created before other objects are created for |
| 475 | /// efficiency. By default, fixed objects are immutable. This returns an |
| 476 | /// index with a negative value. |
| 477 | /// |
| 478 | int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset, |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 479 | bool Immutable, bool isSS) { |
Chris Lattner | 1612faa | 2008-01-25 07:19:06 +0000 | [diff] [blame] | 480 | assert(Size != 0 && "Cannot allocate zero size fixed stack objects!"); |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 481 | Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable, |
| 482 | isSS)); |
Chris Lattner | 1612faa | 2008-01-25 07:19:06 +0000 | [diff] [blame] | 483 | return -++NumFixedObjects; |
| 484 | } |
| 485 | |
| 486 | |
Jakob Stoklund Olesen | 4a0f08c | 2009-08-13 16:19:33 +0000 | [diff] [blame] | 487 | BitVector |
| 488 | MachineFrameInfo::getPristineRegs(const MachineBasicBlock *MBB) const { |
| 489 | assert(MBB && "MBB must be valid"); |
| 490 | const MachineFunction *MF = MBB->getParent(); |
| 491 | assert(MF && "MBB must be part of a MachineFunction"); |
| 492 | const TargetMachine &TM = MF->getTarget(); |
| 493 | const TargetRegisterInfo *TRI = TM.getRegisterInfo(); |
| 494 | BitVector BV(TRI->getNumRegs()); |
| 495 | |
| 496 | // Before CSI is calculated, no registers are considered pristine. They can be |
| 497 | // freely used and PEI will make sure they are saved. |
| 498 | if (!isCalleeSavedInfoValid()) |
| 499 | return BV; |
| 500 | |
| 501 | for (const unsigned *CSR = TRI->getCalleeSavedRegs(MF); CSR && *CSR; ++CSR) |
| 502 | BV.set(*CSR); |
| 503 | |
| 504 | // The entry MBB always has all CSRs pristine. |
| 505 | if (MBB == &MF->front()) |
| 506 | return BV; |
| 507 | |
| 508 | // On other MBBs the saved CSRs are not pristine. |
| 509 | const std::vector<CalleeSavedInfo> &CSI = getCalleeSavedInfo(); |
| 510 | for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(), |
| 511 | E = CSI.end(); I != E; ++I) |
| 512 | BV.reset(I->getReg()); |
| 513 | |
| 514 | return BV; |
| 515 | } |
| 516 | |
| 517 | |
Chris Lattner | d74c556 | 2009-08-23 01:12:47 +0000 | [diff] [blame] | 518 | void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{ |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 519 | if (Objects.empty()) return; |
| 520 | |
Matthijs Kooijman | 0614088 | 2008-11-03 11:16:43 +0000 | [diff] [blame] | 521 | const TargetFrameInfo *FI = MF.getTarget().getFrameInfo(); |
| 522 | int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0); |
Chris Lattner | 9085d8a | 2003-01-16 18:35:57 +0000 | [diff] [blame] | 523 | |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 524 | OS << "Frame Objects:\n"; |
| 525 | |
Chris Lattner | 955fad1 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 526 | for (unsigned i = 0, e = Objects.size(); i != e; ++i) { |
| 527 | const StackObject &SO = Objects[i]; |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 528 | OS << " fi#" << (int)(i-NumFixedObjects) << ": "; |
Evan Cheng | d365312 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 529 | if (SO.Size == ~0ULL) { |
| 530 | OS << "dead\n"; |
| 531 | continue; |
| 532 | } |
Chris Lattner | 955fad1 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 533 | if (SO.Size == 0) |
| 534 | OS << "variable sized"; |
| 535 | else |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 536 | OS << "size=" << SO.Size; |
| 537 | OS << ", align=" << SO.Alignment; |
Alkis Evlogimenos | 76d9dac | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 538 | |
Chris Lattner | 955fad1 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 539 | if (i < NumFixedObjects) |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 540 | OS << ", fixed"; |
Chris Lattner | 955fad1 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 541 | if (i < NumFixedObjects || SO.SPOffset != -1) { |
Chris Lattner | a401b1e | 2007-04-25 04:20:54 +0000 | [diff] [blame] | 542 | int64_t Off = SO.SPOffset - ValOffset; |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 543 | OS << ", at location [SP"; |
Chris Lattner | 9085d8a | 2003-01-16 18:35:57 +0000 | [diff] [blame] | 544 | if (Off > 0) |
Alkis Evlogimenos | 76d9dac | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 545 | OS << "+" << Off; |
Chris Lattner | 9085d8a | 2003-01-16 18:35:57 +0000 | [diff] [blame] | 546 | else if (Off < 0) |
Alkis Evlogimenos | 76d9dac | 2004-09-05 18:41:35 +0000 | [diff] [blame] | 547 | OS << Off; |
Chris Lattner | 955fad1 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 548 | OS << "]"; |
| 549 | } |
| 550 | OS << "\n"; |
| 551 | } |
Chris Lattner | 955fad1 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Chris Lattner | 9085d8a | 2003-01-16 18:35:57 +0000 | [diff] [blame] | 554 | void MachineFrameInfo::dump(const MachineFunction &MF) const { |
David Greene | dc55481 | 2010-01-04 23:39:17 +0000 | [diff] [blame] | 555 | print(MF, dbgs()); |
Chris Lattner | 9085d8a | 2003-01-16 18:35:57 +0000 | [diff] [blame] | 556 | } |
Chris Lattner | 955fad1 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 557 | |
Chris Lattner | 955fad1 | 2002-12-28 20:37:16 +0000 | [diff] [blame] | 558 | //===----------------------------------------------------------------------===// |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 559 | // MachineJumpTableInfo implementation |
| 560 | //===----------------------------------------------------------------------===// |
| 561 | |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 562 | /// getEntrySize - Return the size of each entry in the jump table. |
| 563 | unsigned MachineJumpTableInfo::getEntrySize(const TargetData &TD) const { |
| 564 | // The size of a jump table entry is 4 bytes unless the entry is just the |
| 565 | // address of a block, in which case it is the pointer size. |
| 566 | switch (getEntryKind()) { |
| 567 | case MachineJumpTableInfo::EK_BlockAddress: |
| 568 | return TD.getPointerSize(); |
| 569 | case MachineJumpTableInfo::EK_GPRel32BlockAddress: |
| 570 | case MachineJumpTableInfo::EK_LabelDifference32: |
Chris Lattner | 85fe078 | 2010-01-26 04:05:28 +0000 | [diff] [blame] | 571 | case MachineJumpTableInfo::EK_Custom32: |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 572 | return 4; |
Richard Osborne | 95da605 | 2010-03-11 14:58:16 +0000 | [diff] [blame] | 573 | case MachineJumpTableInfo::EK_Inline: |
| 574 | return 0; |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 575 | } |
| 576 | assert(0 && "Unknown jump table encoding!"); |
| 577 | return ~0; |
| 578 | } |
| 579 | |
| 580 | /// getEntryAlignment - Return the alignment of each entry in the jump table. |
| 581 | unsigned MachineJumpTableInfo::getEntryAlignment(const TargetData &TD) const { |
| 582 | // The alignment of a jump table entry is the alignment of int32 unless the |
| 583 | // entry is just the address of a block, in which case it is the pointer |
| 584 | // alignment. |
| 585 | switch (getEntryKind()) { |
| 586 | case MachineJumpTableInfo::EK_BlockAddress: |
| 587 | return TD.getPointerABIAlignment(); |
| 588 | case MachineJumpTableInfo::EK_GPRel32BlockAddress: |
| 589 | case MachineJumpTableInfo::EK_LabelDifference32: |
Chris Lattner | 85fe078 | 2010-01-26 04:05:28 +0000 | [diff] [blame] | 590 | case MachineJumpTableInfo::EK_Custom32: |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 591 | return TD.getABIIntegerTypeAlignment(32); |
Richard Osborne | 95da605 | 2010-03-11 14:58:16 +0000 | [diff] [blame] | 592 | case MachineJumpTableInfo::EK_Inline: |
| 593 | return 1; |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 594 | } |
| 595 | assert(0 && "Unknown jump table encoding!"); |
| 596 | return ~0; |
| 597 | } |
| 598 | |
Bob Wilson | d1ec31d | 2010-03-18 18:42:41 +0000 | [diff] [blame] | 599 | /// createJumpTableIndex - Create a new jump table entry in the jump table info. |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 600 | /// |
Bob Wilson | d1ec31d | 2010-03-18 18:42:41 +0000 | [diff] [blame] | 601 | unsigned MachineJumpTableInfo::createJumpTableIndex( |
Chris Lattner | a4eb44a | 2006-10-28 18:17:09 +0000 | [diff] [blame] | 602 | const std::vector<MachineBasicBlock*> &DestBBs) { |
Chris Lattner | e7251a0 | 2006-10-28 18:11:20 +0000 | [diff] [blame] | 603 | assert(!DestBBs.empty() && "Cannot create an empty jump table!"); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 604 | JumpTables.push_back(MachineJumpTableEntry(DestBBs)); |
| 605 | return JumpTables.size()-1; |
| 606 | } |
| 607 | |
Dan Gohman | 593ea05 | 2009-04-15 01:18:49 +0000 | [diff] [blame] | 608 | /// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update |
| 609 | /// the jump tables to branch to New instead. |
Chris Lattner | beeb93e | 2010-01-26 05:58:28 +0000 | [diff] [blame] | 610 | bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old, |
| 611 | MachineBasicBlock *New) { |
Dan Gohman | 593ea05 | 2009-04-15 01:18:49 +0000 | [diff] [blame] | 612 | assert(Old != New && "Not making a change?"); |
| 613 | bool MadeChange = false; |
Jim Grosbach | 68bb60f | 2009-11-14 20:09:13 +0000 | [diff] [blame] | 614 | for (size_t i = 0, e = JumpTables.size(); i != e; ++i) |
| 615 | ReplaceMBBInJumpTable(i, Old, New); |
| 616 | return MadeChange; |
| 617 | } |
| 618 | |
| 619 | /// ReplaceMBBInJumpTable - If Old is a target of the jump tables, update |
| 620 | /// the jump table to branch to New instead. |
Chris Lattner | beeb93e | 2010-01-26 05:58:28 +0000 | [diff] [blame] | 621 | bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx, |
| 622 | MachineBasicBlock *Old, |
| 623 | MachineBasicBlock *New) { |
Jim Grosbach | 68bb60f | 2009-11-14 20:09:13 +0000 | [diff] [blame] | 624 | assert(Old != New && "Not making a change?"); |
| 625 | bool MadeChange = false; |
| 626 | MachineJumpTableEntry &JTE = JumpTables[Idx]; |
| 627 | for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j) |
| 628 | if (JTE.MBBs[j] == Old) { |
| 629 | JTE.MBBs[j] = New; |
| 630 | MadeChange = true; |
| 631 | } |
Dan Gohman | 593ea05 | 2009-04-15 01:18:49 +0000 | [diff] [blame] | 632 | return MadeChange; |
| 633 | } |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 634 | |
Chris Lattner | d74c556 | 2009-08-23 01:12:47 +0000 | [diff] [blame] | 635 | void MachineJumpTableInfo::print(raw_ostream &OS) const { |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 636 | if (JumpTables.empty()) return; |
| 637 | |
| 638 | OS << "Jump Tables:\n"; |
| 639 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 640 | for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) { |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 641 | OS << " jt#" << i << ": "; |
| 642 | for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j) |
| 643 | OS << " BB#" << JumpTables[i].MBBs[j]->getNumber(); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 644 | } |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 645 | |
| 646 | OS << '\n'; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 647 | } |
| 648 | |
David Greene | dc55481 | 2010-01-04 23:39:17 +0000 | [diff] [blame] | 649 | void MachineJumpTableInfo::dump() const { print(dbgs()); } |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 650 | |
| 651 | |
| 652 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4d149cd | 2003-01-13 00:23:03 +0000 | [diff] [blame] | 653 | // MachineConstantPool implementation |
| 654 | //===----------------------------------------------------------------------===// |
| 655 | |
Evan Cheng | 9abd7c3 | 2006-09-14 05:50:57 +0000 | [diff] [blame] | 656 | const Type *MachineConstantPoolEntry::getType() const { |
| 657 | if (isMachineConstantPoolEntry()) |
Chris Lattner | cb45963 | 2009-07-21 23:34:23 +0000 | [diff] [blame] | 658 | return Val.MachineCPVal->getType(); |
Evan Cheng | 9abd7c3 | 2006-09-14 05:50:57 +0000 | [diff] [blame] | 659 | return Val.ConstVal->getType(); |
| 660 | } |
| 661 | |
Chris Lattner | cb45963 | 2009-07-21 23:34:23 +0000 | [diff] [blame] | 662 | |
Chris Lattner | 354c016 | 2009-07-21 23:36:01 +0000 | [diff] [blame] | 663 | unsigned MachineConstantPoolEntry::getRelocationInfo() const { |
Chris Lattner | cb45963 | 2009-07-21 23:34:23 +0000 | [diff] [blame] | 664 | if (isMachineConstantPoolEntry()) |
Chris Lattner | 354c016 | 2009-07-21 23:36:01 +0000 | [diff] [blame] | 665 | return Val.MachineCPVal->getRelocationInfo(); |
Chris Lattner | 7cf12c7 | 2009-07-22 00:05:44 +0000 | [diff] [blame] | 666 | return Val.ConstVal->getRelocationInfo(); |
Chris Lattner | cb45963 | 2009-07-21 23:34:23 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 669 | MachineConstantPool::~MachineConstantPool() { |
| 670 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) |
| 671 | if (Constants[i].isMachineConstantPoolEntry()) |
| 672 | delete Constants[i].Val.MachineCPVal; |
| 673 | } |
| 674 | |
Dan Gohman | 83f6120 | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 675 | /// CanShareConstantPoolEntry - Test whether the given two constants |
| 676 | /// can be allocated the same constant pool entry. |
| 677 | static bool CanShareConstantPoolEntry(Constant *A, Constant *B, |
| 678 | const TargetData *TD) { |
| 679 | // Handle the trivial case quickly. |
| 680 | if (A == B) return true; |
| 681 | |
| 682 | // If they have the same type but weren't the same constant, quickly |
| 683 | // reject them. |
| 684 | if (A->getType() == B->getType()) return false; |
| 685 | |
| 686 | // For now, only support constants with the same size. |
| 687 | if (TD->getTypeStoreSize(A->getType()) != TD->getTypeStoreSize(B->getType())) |
| 688 | return false; |
| 689 | |
| 690 | // If a floating-point value and an integer value have the same encoding, |
| 691 | // they can share a constant-pool entry. |
| 692 | if (ConstantFP *AFP = dyn_cast<ConstantFP>(A)) |
| 693 | if (ConstantInt *BI = dyn_cast<ConstantInt>(B)) |
| 694 | return AFP->getValueAPF().bitcastToAPInt() == BI->getValue(); |
| 695 | if (ConstantFP *BFP = dyn_cast<ConstantFP>(B)) |
| 696 | if (ConstantInt *AI = dyn_cast<ConstantInt>(A)) |
| 697 | return BFP->getValueAPF().bitcastToAPInt() == AI->getValue(); |
| 698 | |
| 699 | // Two vectors can share an entry if each pair of corresponding |
| 700 | // elements could. |
| 701 | if (ConstantVector *AV = dyn_cast<ConstantVector>(A)) |
| 702 | if (ConstantVector *BV = dyn_cast<ConstantVector>(B)) { |
| 703 | if (AV->getType()->getNumElements() != BV->getType()->getNumElements()) |
| 704 | return false; |
| 705 | for (unsigned i = 0, e = AV->getType()->getNumElements(); i != e; ++i) |
Dan Gohman | 60d686d | 2009-10-31 14:12:53 +0000 | [diff] [blame] | 706 | if (!CanShareConstantPoolEntry(AV->getOperand(i), |
| 707 | BV->getOperand(i), TD)) |
Dan Gohman | 83f6120 | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 708 | return false; |
| 709 | return true; |
| 710 | } |
| 711 | |
| 712 | // TODO: Handle other cases. |
| 713 | |
| 714 | return false; |
| 715 | } |
| 716 | |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 717 | /// getConstantPoolIndex - Create a new entry in the constant pool or return |
Dan Gohman | 05ae983 | 2008-09-16 20:45:53 +0000 | [diff] [blame] | 718 | /// an existing one. User must specify the log2 of the minimum required |
| 719 | /// alignment for the object. |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 720 | /// |
| 721 | unsigned MachineConstantPool::getConstantPoolIndex(Constant *C, |
| 722 | unsigned Alignment) { |
| 723 | assert(Alignment && "Alignment must be specified!"); |
| 724 | if (Alignment > PoolAlignment) PoolAlignment = Alignment; |
Dan Gohman | 83f6120 | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 725 | |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 726 | // Check to see if we already have this constant. |
| 727 | // |
| 728 | // FIXME, this could be made much more efficient for large constant pools. |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 729 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) |
Dan Gohman | 83f6120 | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 730 | if (!Constants[i].isMachineConstantPoolEntry() && |
| 731 | CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, TD)) { |
| 732 | if ((unsigned)Constants[i].getAlignment() < Alignment) |
| 733 | Constants[i].Alignment = Alignment; |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 734 | return i; |
Dan Gohman | 83f6120 | 2009-10-28 01:12:16 +0000 | [diff] [blame] | 735 | } |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 736 | |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 737 | Constants.push_back(MachineConstantPoolEntry(C, Alignment)); |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 738 | return Constants.size()-1; |
| 739 | } |
| 740 | |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 741 | unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V, |
| 742 | unsigned Alignment) { |
| 743 | assert(Alignment && "Alignment must be specified!"); |
| 744 | if (Alignment > PoolAlignment) PoolAlignment = Alignment; |
| 745 | |
| 746 | // Check to see if we already have this constant. |
| 747 | // |
| 748 | // FIXME, this could be made much more efficient for large constant pools. |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 749 | int Idx = V->getExistingMachineCPValue(this, Alignment); |
| 750 | if (Idx != -1) |
| 751 | return (unsigned)Idx; |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 752 | |
| 753 | Constants.push_back(MachineConstantPoolEntry(V, Alignment)); |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 754 | return Constants.size()-1; |
| 755 | } |
| 756 | |
Chris Lattner | 62ca325 | 2008-08-23 22:53:13 +0000 | [diff] [blame] | 757 | void MachineConstantPool::print(raw_ostream &OS) const { |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 758 | if (Constants.empty()) return; |
| 759 | |
| 760 | OS << "Constant Pool:\n"; |
Evan Cheng | b8973bd | 2006-01-31 22:23:14 +0000 | [diff] [blame] | 761 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 762 | OS << " cp#" << i << ": "; |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 763 | if (Constants[i].isMachineConstantPoolEntry()) |
| 764 | Constants[i].Val.MachineCPVal->print(OS); |
| 765 | else |
| 766 | OS << *(Value*)Constants[i].Val.ConstVal; |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 767 | OS << ", align=" << Constants[i].getAlignment(); |
Evan Cheng | b8973bd | 2006-01-31 22:23:14 +0000 | [diff] [blame] | 768 | OS << "\n"; |
| 769 | } |
Chris Lattner | 4d149cd | 2003-01-13 00:23:03 +0000 | [diff] [blame] | 770 | } |
| 771 | |
David Greene | dc55481 | 2010-01-04 23:39:17 +0000 | [diff] [blame] | 772 | void MachineConstantPool::dump() const { print(dbgs()); } |