llvm.org GIT mirror llvm / fb8075d
Add a quick and dirty "loop aligner pass". x86 uses it to align its loops to 16-byte boundaries. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47703 91177308-0d34-0410-b5e6-96231b3b80d8 Evan Cheng 12 years ago
25 changed file(s) with 173 addition(s) and 50 deletion(s). Raw diff Collapse all Expand all
7777 /// CurrentSection - The current section we are emitting to. This is
7878 /// controlled and used by the SwitchSection method.
7979 std::string CurrentSection;
80
81 /// IsInTextSection - True if the current section we are emitting to is a
82 /// text section.
83 bool IsInTextSection;
8084
8185 protected:
8286 AsmPrinter(std::ostream &o, TargetMachine &TM, const TargetAsmInfo *T);
268272 /// an explicit alignment requested, it will unconditionally override the
269273 /// alignment request. However, if ForcedAlignBits is specified, this value
270274 /// has final say: the ultimate alignment will be the max of ForcedAlignBits
271 /// and the alignment computed with NumBits and the global. If UseFillExpr
272 /// is true, it also emits an optional second value FillValue which the
273 /// assembler uses to fill gaps to match alignment.
275 /// and the alignment computed with NumBits and the global
274276 ///
275277 /// The algorithm is:
276278 /// Align = NumBits;
278280 /// Align = std::max(Align, ForcedAlignBits);
279281 ///
280282 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0,
281 unsigned ForcedAlignBits = 0, bool UseFillExpr = false,
282 unsigned FillValue = 0) const;
283 unsigned ForcedAlignBits = 0) const;
283284
284285 /// printLabel - This method prints a local label used by debug and
285286 /// exception handling tables.
316317 /// printBasicBlockLabel - This method prints the label for the specified
317318 /// MachineBasicBlock
318319 virtual void printBasicBlockLabel(const MachineBasicBlock *MBB,
320 bool printAlign = false,
319321 bool printColon = false,
320322 bool printComment = true) const;
321323
7474 /// LiveIns - Keep track of the physical registers that are livein of
7575 /// the basicblock.
7676 std::vector LiveIns;
77
78 /// Alignment - Alignment of the basic block. Zero if the basic block does
79 /// not need to be aligned.
80 unsigned Alignment;
7781
7882 /// IsLandingPad - Indicate that this basic block is entered via an
7983 /// exception handler.
8185
8286 public:
8387 explicit MachineBasicBlock(const BasicBlock *bb = 0)
84 : Prev(0), Next(0), BB(bb), Number(-1), xParent(0), IsLandingPad(false) {
88 : Prev(0), Next(0), BB(bb), Number(-1), xParent(0),
89 Alignment(0), IsLandingPad(false) {
8590 Insts.parent = this;
8691 }
8792
180185 const_livein_iterator livein_end() const { return LiveIns.end(); }
181186 bool livein_empty() const { return LiveIns.empty(); }
182187
188 /// getAlignment - Return alignment of the basic block.
189 ///
190 unsigned getAlignment() const { return Alignment; }
191
192 /// setAlignment - Set alignment of the basic block.
193 ///
194 void setAlignment(unsigned Align) { Alignment = Align; }
195
183196 /// isLandingPad - Returns true if the block is a landing pad. That is
184197 /// this basic block is entered via an exception handler.
185198 bool isLandingPad() const { return IsLandingPad; }
203203 }
204204
205205 /// getObjectAlignment - Return the alignment of the specified stack object...
206 int getObjectAlignment(int ObjectIdx) const {
206 unsigned getObjectAlignment(int ObjectIdx) const {
207207 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
208208 "Invalid Object Idx!");
209209 return Objects[ObjectIdx+NumFixedObjects].Alignment;
128128 /// IfConverter Pass - This pass performs machine code if conversion.
129129 FunctionPass *createIfConverterPass();
130130
131 /// LoopAligner Pass - This pass aligns loop headers to target specific
132 /// alignment boundary.
133 FunctionPass *createLoopAlignerPass();
134
131135 /// DebugLabelFoldingPass - This pass prunes out redundant debug labels. This
132136 /// allows a debug emitter to determine if the range of two labels is empty,
133137 /// by seeing if the labels map to the same reduced label.
163163 /// boundary.
164164 bool AlignmentIsInBytes; // Defaults to true
165165
166 /// TextAlignFillValue - If non-zero, this is used to fill the executable
167 /// space created as the result of a alignment directive.
168 unsigned TextAlignFillValue;
169
166170 //===--- Section Switching Directives ---------------------------------===//
167171
168172 /// SwitchToSectionDirective - This is the directive used when we want to
502506 bool getAlignmentIsInBytes() const {
503507 return AlignmentIsInBytes;
504508 }
509 unsigned getTextAlignFillValue() const {
510 return TextAlignFillValue;
511 }
505512 const char *getSwitchToSectionDirective() const {
506513 return SwitchToSectionDirective;
507514 }
547547
548548 /// getIfCvtBlockLimit - returns the target specific if-conversion block size
549549 /// limit. Any block whose size is greater should not be predicated.
550 virtual unsigned getIfCvtBlockSizeLimit() const {
550 unsigned getIfCvtBlockSizeLimit() const {
551551 return IfCvtBlockSizeLimit;
552552 }
553553
554554 /// getIfCvtDupBlockLimit - returns the target specific size limit for a
555555 /// block to be considered for duplication. Any block whose size is greater
556556 /// should not be duplicated to facilitate its predication.
557 virtual unsigned getIfCvtDupBlockSizeLimit() const {
557 unsigned getIfCvtDupBlockSizeLimit() const {
558558 return IfCvtDupBlockSizeLimit;
559559 }
560560
561 /// getPrefLoopAlignment - return the preferred loop alignment.
562 ///
563 unsigned getPrefLoopAlignment() const {
564 return PrefLoopAlignment;
565 }
566
561567 /// getPreIndexedAddressParts - returns true by value, base pointer and
562568 /// offset pointer and addressing mode by reference if the node's address
563569 /// can be legally represented as pre-indexed load / store address.
582588 /// jumptable.
583589 virtual SDOperand getPICJumpTableRelocBase(SDOperand Table,
584590 SelectionDAG &DAG) const;
585
591
586592 //===--------------------------------------------------------------------===//
587593 // TargetLowering Optimization Methods
588594 //
889895 void setIfCvtDupBlockSizeLimit(unsigned Limit) {
890896 IfCvtDupBlockSizeLimit = Limit;
891897 }
898
899 /// setPrefLoopAlignment - Set the target's preferred loop alignment. Default
900 /// alignment is zero, it means the target does not care about loop alignment.
901 void setPrefLoopAlignment(unsigned Align) {
902 PrefLoopAlignment = Align;
903 }
892904
893905 public:
894906
12751287 /// duplicated during if-conversion.
12761288 unsigned IfCvtDupBlockSizeLimit;
12771289
1290 /// PrefLoopAlignment - The perferred loop alignment.
1291 ///
1292 unsigned PrefLoopAlignment;
1293
12781294 /// StackPointerRegisterToSaveRestore - If set to a physical register, this
12791295 /// specifies the register that llvm.savestack/llvm.restorestack should save
12801296 /// and restore.
3838 char AsmPrinter::ID = 0;
3939 AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
4040 const TargetAsmInfo *T)
41 : MachineFunctionPass((intptr_t)&ID), FunctionNumber(0), O(o), TM(tm), TAI(T)
41 : MachineFunctionPass((intptr_t)&ID), FunctionNumber(0), O(o), TM(tm), TAI(T),
42 IsInTextSection(false)
4243 {}
4344
4445 std::string AsmPrinter::getSectionForFunction(const Function &F) const {
6869
6970 if (!CurrentSection.empty())
7071 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
72
73 IsInTextSection = true;
7174 }
7275
7376 /// SwitchToDataSection - Switch to the specified data section of the executable
9295
9396 if (!CurrentSection.empty())
9497 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
98
99 IsInTextSection = false;
95100 }
96101
97102
343348 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
344349 << '_' << uid << "_set_" << MBB->getNumber();
345350 } else {
346 printBasicBlockLabel(MBB, false, false);
351 printBasicBlockLabel(MBB, false, false, false);
347352 // If the arch uses custom Jump Table directives, don't calc relative to
348353 // JT
349354 if (!HadJTEntryDirective)
351356 << getFunctionNumber() << '_' << uid;
352357 }
353358 } else {
354 printBasicBlockLabel(MBB, false, false);
359 printBasicBlockLabel(MBB, false, false, false);
355360 }
356361 }
357362
678683 // Align = std::max(Align, ForcedAlignBits);
679684 //
680685 void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
681 unsigned ForcedAlignBits, bool UseFillExpr,
682 unsigned FillValue) const {
686 unsigned ForcedAlignBits) const {
683687 if (GV && GV->getAlignment())
684688 NumBits = Log2_32(GV->getAlignment());
685689 NumBits = std::max(NumBits, ForcedAlignBits);
687691 if (NumBits == 0) return; // No need to emit alignment.
688692 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
689693 O << TAI->getAlignDirective() << NumBits;
694
695 unsigned FillValue = TAI->getTextAlignFillValue();
696 bool UseFillExpr = IsInTextSection && FillValue;
690697 if (UseFillExpr) O << ",0x" << std::hex << FillValue << std::dec;
691698 O << "\n";
692699 }
12511258
12521259 if (Modifier[0]=='l') // labels are target independent
12531260 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
1254 false, false);
1261 false, false, false);
12551262 else {
12561263 AsmPrinter *AP = const_cast(this);
12571264 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
13171324 /// printBasicBlockLabel - This method prints the label for the specified
13181325 /// MachineBasicBlock
13191326 void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
1327 bool printAlign,
13201328 bool printColon,
13211329 bool printComment) const {
1330 if (printAlign) {
1331 unsigned Align = MBB->getAlignment();
1332 if (Align)
1333 EmitAlignment(Log2_32(Align));
1334 }
1335
13221336 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << "_"
13231337 << MBB->getNumber();
13241338 if (printColon)
13371351
13381352 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
13391353 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
1340 printBasicBlockLabel(MBB, false, false);
1354 printBasicBlockLabel(MBB, false, false, false);
13411355 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
13421356 << '_' << uid << '\n';
13431357 }
13501364 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
13511365 << getFunctionNumber() << '_' << uid << '_' << uid2
13521366 << "_set_" << MBB->getNumber() << ',';
1353 printBasicBlockLabel(MBB, false, false);
1367 printBasicBlockLabel(MBB, false, false, false);
13541368 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
13551369 << '_' << uid << '_' << uid2 << '\n';
13561370 }
4343 cl::desc("Max number of predecessors to consider tail merging"),
4444 cl::init(100), cl::Hidden);
4545
46 struct BranchFolder : public MachineFunctionPass {
46 struct VISIBILITY_HIDDEN BranchFolder : public MachineFunctionPass {
4747 static char ID;
4848 explicit BranchFolder(bool defaultEnableTailMerge) :
4949 MachineFunctionPass((intptr_t)&ID) {
5555 STATISTIC(NumDupBBs, "Number of duplicated blocks");
5656
5757 namespace {
58 class IfConverter : public MachineFunctionPass {
58 class VISIBILITY_HIDDEN IfConverter : public MachineFunctionPass {
5959 enum IfcvtKind {
6060 ICNotClassfied, // BB data valid, but not classified.
6161 ICSimpleFalse, // Same as ICSimple, but on the false path.
0 //===-- LoopAligner.cpp - Loop aligner pass. ------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the pass that align loop headers to target specific
10 // alignment boundary.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "loopalign"
15 #include "llvm/CodeGen/MachineLoopInfo.h"
16 #include "llvm/CodeGen/MachineFunctionPass.h"
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/Target/TargetLowering.h"
19 #include "llvm/Target/TargetMachine.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/Debug.h"
22 using namespace llvm;
23
24 namespace {
25 class LoopAligner : public MachineFunctionPass {
26 const TargetLowering *TLI;
27
28 public:
29 static char ID;
30 LoopAligner() : MachineFunctionPass((intptr_t)&ID) {}
31
32 virtual bool runOnMachineFunction(MachineFunction &MF);
33 virtual const char *getPassName() const { return "Loop aligner"; }
34
35 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
36 AU.addRequired();
37 AU.addPreserved();
38 MachineFunctionPass::getAnalysisUsage(AU);
39 }
40 };
41
42 char LoopAligner::ID = 0;
43 } // end anonymous namespace
44
45 FunctionPass *llvm::createLoopAlignerPass() { return new LoopAligner(); }
46
47 bool LoopAligner::runOnMachineFunction(MachineFunction &MF) {
48 const MachineLoopInfo *MLI = &getAnalysis();
49
50 if (MLI->begin() == MLI->end())
51 return false; // No loops.
52
53 unsigned Align = MF.getTarget().getTargetLowering()->getPrefLoopAlignment();
54 if (!Align)
55 return false; // Don't care about loop alignment.
56
57 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
58 MachineBasicBlock *MBB = I;
59 if (MLI->isLoopHeader(MBB))
60 MBB->setAlignment(Align);
61 }
62
63 return true;
64 }
165165 if (LBB) OS << LBB->getName() << ": ";
166166 OS << (const void*)this
167167 << ", LLVM BB @" << (const void*) LBB << ", ID#" << getNumber();
168 if (Alignment) OS << ", Alignment " << Alignment;
168169 if (isLandingPad()) OS << ", EH LANDING PAD";
169170 OS << ":\n";
170171
205205 JumpBufSize = 0;
206206 JumpBufAlignment = 0;
207207 IfCvtBlockSizeLimit = 2;
208 IfCvtDupBlockSizeLimit = 0;
209 PrefLoopAlignment = 0;
208210
209211 InitLibcallNames(LibcallRoutineNames);
210212 InitCmpLibcallCCs(CmpLibcallCCs);
247247 I != E; ++I) {
248248 // Print a label for the basic block.
249249 if (I != MF.begin()) {
250 printBasicBlockLabel(I, true);
250 printBasicBlockLabel(I, true, true);
251251 O << '\n';
252252 }
253253 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
709709 << '_' << JTI << '_' << MO2.getImm()
710710 << "_set_" << MBB->getNumber();
711711 else if (TM.getRelocationModel() == Reloc::PIC_) {
712 printBasicBlockLabel(MBB, false, false);
712 printBasicBlockLabel(MBB, false, false, false);
713713 // If the arch uses custom Jump Table directives, don't calc relative to JT
714714 if (!TAI->getJumpTableDirective())
715715 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
716716 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImm();
717717 } else
718 printBasicBlockLabel(MBB, false, false);
718 printBasicBlockLabel(MBB, false, false, false);
719719 if (i != e-1)
720720 O << '\n';
721721 }
170170 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
171171 I != E; ++I) {
172172 if (I != MF.begin()) {
173 printBasicBlockLabel(I, true);
173 printBasicBlockLabel(I, true, true);
174174 O << '\n';
175175 }
176176 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
459459 I != E; ++I) {
460460 // Print a label for the basic block.
461461 if (I != MF.begin()) {
462 printBasicBlockLabel(I, true);
462 printBasicBlockLabel(I, true, true);
463463 O << '\n';
464464 }
465465 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
148148 I != E; ++I) {
149149 // Print a label for the basic block if there are any predecessors.
150150 if (!I->pred_empty()) {
151 printBasicBlockLabel(I, true);
151 printBasicBlockLabel(I, true, true);
152152 O << '\n';
153153 }
154154 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
296296
297297 // Print a label for the basic block.
298298 if (I != MF.begin()) {
299 printBasicBlockLabel(I, true);
299 printBasicBlockLabel(I, true, true);
300300 O << '\n';
301301 }
302302
603603 I != E; ++I) {
604604 // Print a label for the basic block.
605605 if (I != MF.begin()) {
606 printBasicBlockLabel(I, true);
606 printBasicBlockLabel(I, true, true);
607607 O << '\n';
608608 }
609609 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
837837 I != E; ++I) {
838838 // Print a label for the basic block.
839839 if (I != MF.begin()) {
840 printBasicBlockLabel(I, true);
840 printBasicBlockLabel(I, true, true);
841841 O << '\n';
842842 }
843843 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
115115 I != E; ++I) {
116116 // Print a label for the basic block.
117117 if (I != MF.begin()) {
118 printBasicBlockLabel(I, true);
118 printBasicBlockLabel(I, true, true);
119119 O << '\n';
120120 }
121121 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
100100 switch (F->getLinkage()) {
101101 default: assert(0 && "Unknown linkage type!");
102102 case Function::InternalLinkage: // Symbols default to internal.
103 if (Subtarget->isTargetDarwin())
104 // FIXME: This should be parameterized somewhere.
105 EmitAlignment(4, F, 0, true, 0x90);
106 else
107 EmitAlignment(4, F);
103 EmitAlignment(4, F);
108104 break;
109105 case Function::DLLExportLinkage:
110106 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
111107 //FALLS THROUGH
112108 case Function::ExternalLinkage:
113 if (Subtarget->isTargetDarwin())
114 // FIXME: This should be parameterized somewhere.
115 EmitAlignment(4, F, 0, true, 0x90);
116 else
117 EmitAlignment(4, F);
109 EmitAlignment(4, F);
118110 O << "\t.globl\t" << CurrentFnName << "\n";
119111 break;
120112 case Function::LinkOnceLinkage:
121113 case Function::WeakLinkage:
114 EmitAlignment(4, F);
122115 if (Subtarget->isTargetDarwin()) {
123 // FIXME: This should be parameterized somewhere.
124 EmitAlignment(4, F, 0, true, 0x90);
125116 O << "\t.globl\t" << CurrentFnName << "\n";
126117 O << TAI->getWeakDefDirective() << CurrentFnName << "\n";
127118 } else if (Subtarget->isTargetCygMing()) {
128 EmitAlignment(4, F);
129119 O << "\t.globl\t" << CurrentFnName << "\n";
130120 O << "\t.linkonce discard\n";
131121 } else {
132 EmitAlignment(4, F);
133122 O << "\t.weak\t" << CurrentFnName << "\n";
134123 }
135124 break;
179168 I != E; ++I) {
180169 // Print a label for the basic block.
181170 if (!I->pred_empty()) {
182 printBasicBlockLabel(I, true);
171 printBasicBlockLabel(I, true, true);
183172 O << '\n';
184173 }
185174 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
514503
515504 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
516505 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
517 printBasicBlockLabel(MBB, false, false);
506 printBasicBlockLabel(MBB, false, false, false);
518507 if (Subtarget->isPICStyleRIPRel())
519508 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
520509 << '_' << uid << '\n';
542531 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
543532 << '_' << uid << "_set_" << MBB->getNumber();
544533 } else if (Subtarget->isPICStyleGOT()) {
545 printBasicBlockLabel(MBB, false, false);
534 printBasicBlockLabel(MBB, false, false, false);
546535 O << "@GOTOFF";
547536 } else
548537 assert(0 && "Don't know how to print MBB label for this PIC mode");
549538 } else
550 printBasicBlockLabel(MBB, false, false);
539 printBasicBlockLabel(MBB, false, false, false);
551540 }
552541
553542 bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
713713 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
714714 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
715715 allowUnalignedMemoryAccesses = true; // x86 supports it!
716 setPrefLoopAlignment(16);
716717 }
717718
718719 /// getMaxByValAlign - Helper for getByValTypeAlignment to determine
7777 I != E; ++I) {
7878 // Print a label for the basic block if there are any predecessors.
7979 if (!I->pred_empty()) {
80 printBasicBlockLabel(I, true);
80 printBasicBlockLabel(I, true, true);
8181 O << '\n';
8282 }
8383 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
241241
242242 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
243243 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
244 printBasicBlockLabel(MBB, false, false);
244 printBasicBlockLabel(MBB, false, false, false);
245245 O << '-' << "\"L" << getFunctionNumber() << "$pb\"'\n";
246246 }
247247
4646 switch (Subtarget->TargetType) {
4747 case X86Subtarget::isDarwin:
4848 AlignmentIsInBytes = false;
49 TextAlignFillValue = 0x90;
4950 GlobalPrefix = "_";
5051 if (!Subtarget->is64Bit())
5152 Data64bitsDirective = 0; // we can't emit a 64-bit unit
163163 return true; // -print-machineinstr should print after this.
164164 }
165165
166 bool X86TargetMachine::addPreEmitPass(FunctionPassManager &PM, bool Fast) {
167 if (Fast) return false;
168
169 PM.add(createLoopAlignerPass());
170 return true;
171 }
172
166173 bool X86TargetMachine::addAssemblyEmitter(FunctionPassManager &PM, bool Fast,
167174 std::ostream &Out) {
168175 PM.add(createX86CodePrinterPass(Out, *this));
6262 // Set up the pass pipeline.
6363 virtual bool addInstSelector(FunctionPassManager &PM, bool Fast);
6464 virtual bool addPostRegAlloc(FunctionPassManager &PM, bool Fast);
65 virtual bool addPreEmitPass(FunctionPassManager &PM, bool Fast);
6566 virtual bool addAssemblyEmitter(FunctionPassManager &PM, bool Fast,
6667 std::ostream &Out);
6768 virtual bool addCodeEmitter(FunctionPassManager &PM, bool Fast,