blob: eb02b4f68a2d4e0d8313c0aa1cb28a20430373dc [file] [log] [blame]
Evan Chengb9803a82009-11-06 23:52:48 +00001//===-- ARMExpandPseudoInsts.cpp - Expand pseudo instructions -----*- C++ -*-=//
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 a pass that expand pseudo instructions into target
11// instructions to allow proper scheduling, if-conversion, and other late
12// optimizations. This pass should be run after register allocation but before
13// post- regalloc scheduling pass.
14//
15//===----------------------------------------------------------------------===//
16
17#define DEBUG_TYPE "arm-pseudo"
18#include "ARM.h"
19#include "ARMBaseInstrInfo.h"
20#include "llvm/CodeGen/MachineFunctionPass.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22
23using namespace llvm;
24
25namespace {
26 class ARMExpandPseudo : public MachineFunctionPass {
27 public:
28 static char ID;
29 ARMExpandPseudo() : MachineFunctionPass(&ID) {}
30
31 const TargetInstrInfo *TII;
32
33 virtual bool runOnMachineFunction(MachineFunction &Fn);
34
35 virtual const char *getPassName() const {
36 return "ARM pseudo instruction expansion pass";
37 }
38
39 private:
40 bool ExpandMBB(MachineBasicBlock &MBB);
41 };
42 char ARMExpandPseudo::ID = 0;
43}
44
45bool ARMExpandPseudo::ExpandMBB(MachineBasicBlock &MBB) {
46 bool Modified = false;
47
48 MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
49 while (MBBI != E) {
50 MachineInstr &MI = *MBBI;
Chris Lattner7896c9f2009-12-03 00:50:42 +000051 MachineBasicBlock::iterator NMBBI = llvm::next(MBBI);
Evan Chengb9803a82009-11-06 23:52:48 +000052
53 unsigned Opcode = MI.getOpcode();
54 switch (Opcode) {
55 default: break;
56 case ARM::tLDRpci_pic:
57 case ARM::t2LDRpci_pic: {
58 unsigned NewLdOpc = (Opcode == ARM::tLDRpci_pic)
59 ? ARM::tLDRpci : ARM::t2LDRpci;
60 unsigned DstReg = MI.getOperand(0).getReg();
61 if (!MI.getOperand(0).isDead()) {
62 MachineInstr *NewMI =
63 AddDefaultPred(BuildMI(MBB, MBBI, MI.getDebugLoc(),
64 TII->get(NewLdOpc), DstReg)
65 .addOperand(MI.getOperand(1)));
66 NewMI->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
67 BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(ARM::tPICADD))
68 .addReg(DstReg, getDefRegState(true))
69 .addReg(DstReg)
70 .addOperand(MI.getOperand(2));
71 }
72 MI.eraseFromParent();
73 Modified = true;
74 break;
75 }
76 case ARM::t2MOVi32imm: {
77 unsigned DstReg = MI.getOperand(0).getReg();
Evan Chengb9803a82009-11-06 23:52:48 +000078 if (!MI.getOperand(0).isDead()) {
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +000079 const MachineOperand &MO = MI.getOperand(1);
80 MachineInstrBuilder LO16, HI16;
81
82 LO16 = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(ARM::t2MOVi16),
83 DstReg);
84 HI16 = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(ARM::t2MOVTi16))
85 .addReg(DstReg, getDefRegState(true)).addReg(DstReg);
86
87 if (MO.isImm()) {
88 unsigned Imm = MO.getImm();
89 unsigned Lo16 = Imm & 0xffff;
90 unsigned Hi16 = (Imm >> 16) & 0xffff;
91 LO16 = LO16.addImm(Lo16);
92 HI16 = HI16.addImm(Hi16);
93 } else {
Dan Gohman46510a72010-04-15 01:51:59 +000094 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +000095 unsigned TF = MO.getTargetFlags();
96 LO16 = LO16.addGlobalAddress(GV, MO.getOffset(), TF | ARMII::MO_LO16);
97 HI16 = HI16.addGlobalAddress(GV, MO.getOffset(), TF | ARMII::MO_HI16);
98 // FIXME: What's about memoperands?
99 }
100 AddDefaultPred(LO16);
101 AddDefaultPred(HI16);
Evan Chengb9803a82009-11-06 23:52:48 +0000102 }
103 MI.eraseFromParent();
104 Modified = true;
105 }
Evan Chengb9803a82009-11-06 23:52:48 +0000106 }
107 MBBI = NMBBI;
108 }
109
110 return Modified;
111}
112
113bool ARMExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
114 TII = MF.getTarget().getInstrInfo();
115
116 bool Modified = false;
117 for (MachineFunction::iterator MFI = MF.begin(), E = MF.end(); MFI != E;
118 ++MFI)
119 Modified |= ExpandMBB(*MFI);
120 return Modified;
121}
122
123/// createARMExpandPseudoPass - returns an instance of the pseudo instruction
124/// expansion pass.
125FunctionPass *llvm::createARMExpandPseudoPass() {
126 return new ARMExpandPseudo();
127}